﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.BusinessService;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.Permission;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Log;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper.Report;
using Kingdee.BOS.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace DzInfo.XH.FormPlugIn
{
    [Description("茂名新辉客户往来账表单插件"), HotUpdate]
    public class MmReport : AbstractDynamicFormPlugIn
    {
        //对账单（副表）a15f3164-5250-41a3-a660-f2980b0fde0b
        private readonly string t2 = "a15f3164-5250-41a3-a660-f2980b0fde0b";
        //明细对账单（副表）99d8cfec-e330-45a7-a1f4-800a1481a27b
        private readonly string t3 = "99d8cfec-e330-45a7-a1f4-800a1481a27b";
        //客户品种销售ae5e68ce-daae-4eb6-8576-bc2b993d688b
        private readonly string t4 = "ae5e68ce-daae-4eb6-8576-bc2b993d688b";
        //销售分类    335f09c4-f80a-46a2-9f81-f9cf2247a59b
        private readonly string t5 = "335f09c4-f80a-46a2-9f81-f9cf2247a59b";
        public override void TabItemSelectedChange(TabItemSelectedChangeEventArgs e)
        {
            if (BindData()) { return; }
            base.TabItemSelectedChange(e);
            JSONArray setting = this.GetSetupInfo(this.View.BusinessInfo.GetForm().Id);
            var tKey = e.TabKey;
            if (tKey.Equals("F_POSW_TAB_T7S_P0_T7S"))
            {
                this.Model.SetValue("FTDMB", 0, 0);
                this.View.UpdateView("FTDMB");

                if (setting != null)
                {
                    // 将 JSONArray 转换为 JArray 以便处理
                    JArray settingsArray = JArray.Parse(setting.ToString());
                    // 1. 删除所有 key 为 "advantageSetting" 的对象
                    var itemsToRemove2 = settingsArray.Where(item => item["key"] != null && item["key"].ToString() == "advantageSetting").ToList();
                    foreach (var item in itemsToRemove2)
                    {
                        settingsArray.Remove(item);
                    }
                    JObject newAdvantageSetting2 = new JObject
                    {
                        ["key"] = "advantageSetting",
                        ["enableadvantage"] = true,
                        ["title"] = null,
                        ["filter"] = null,
                        ["templateValue"] = "", // 生成新的 GUID
                        ["rptValue"] = "",
                        ["filterSetting"] = null,
                        ["printerFID"] = null,
                        ["selectentity"] = null,
                        ["sortString"] = null,
                        ["sortsqlString"] = null,
                        ["sortConditon"] = null,
                        ["economizePaper"] = false,
                        ["usePaperInfo"] = false,
                        ["groupString"] = null,
                        ["groupSettingName"] = null,
                        ["GroupSortString"] = null
                    }
                ;

                    settingsArray.Add(newAdvantageSetting2);
                    // 1. 删除所有 key 为 "notePrintParameterSetting" 的对象
                    var itemsToRemove = settingsArray.Where(item => item["key"] != null && item["key"].ToString() == "notePrintParameterSetting").ToList();
                    foreach (var item in itemsToRemove)
                    {
                        settingsArray.Remove(item);
                    }
                    //重新添加notePrintParameterSetting
                    JObject newAdvantageSetting = new JObject
                    {
                        ["key"] = "notePrintParameterSetting",
                        ["batchPrintPageCount"] = 0,
                        ["showSelectTemplate"] = true,
                        ["listSetPrintNumber"] = false,
                        ["usePaperInfo"] = false,
                        ["paperType"] = "",
                        ["paperOrientation"] = "",
                        ["paperHeight"] = 0,
                        ["paperWidth"] = 0,
                        ["marginTop"] = 0,
                        ["marginBottom"] = 0,
                        ["marginLeft"] = 0,
                        ["marginRight"] = 0,
                        ["singleBillPrint"] = false,
                        ["multTemplatePrintByBill"] = false,
                        ["printFirstFilter"] = false,
                        ["selectEntryFilter"] = false
                    }
                ;

                    settingsArray.Add(newAdvantageSetting);



                    // 将修改后的 JArray 转回 JSONArray
                    setting = new JSONArray(settingsArray.ToString());
                }
                PrintServiceHelper.SaveNotePrintPara(this.Context, this.View.BusinessInfo.GetForm().Id, this.Context.UserId, setting.ToJSONString());
            }
            if (tKey.Equals("F_POSW_TAB_T7S_P1_T7S"))
            {
                this.Model.SetValue("FTDMB", 1, 0);
                this.View.UpdateView("FTDMB");

                if (setting != null)
                {
                    // 将 JSONArray 转换为 JArray 以便处理
                    JArray settingsArray = JArray.Parse(setting.ToString());
                    // 1. 删除所有 key 为 "notePrintParameterSetting" 的对象
                    var itemsToRemove2 = settingsArray.Where(item => item["key"] != null && item["key"].ToString() == "notePrintParameterSetting").ToList();
                    foreach (var item in itemsToRemove2)
                    {
                        settingsArray.Remove(item);
                    }
                    //重新添加notePrintParameterSetting
                    JObject newAdvantageSetting2 = new JObject
                    {
                        ["key"] = "notePrintParameterSetting",
                        ["batchPrintPageCount"] = 0,
                        ["showSelectTemplate"] = false,
                        ["listSetPrintNumber"] = false,
                        ["usePaperInfo"] = false,
                        ["paperType"] = "",
                        ["paperOrientation"] = "",
                        ["paperHeight"] = 0,
                        ["paperWidth"] = 0,
                        ["marginTop"] = 0,
                        ["marginBottom"] = 0,
                        ["marginLeft"] = 0,
                        ["marginRight"] = 0,
                        ["singleBillPrint"] = false,
                        ["multTemplatePrintByBill"] = false,
                        ["printFirstFilter"] = false,
                        ["selectEntryFilter"] = false
                    }
                ;

                    settingsArray.Add(newAdvantageSetting2);

                    // 1. 删除所有 key 为 "advantageSetting" 的对象
                    var itemsToRemove = settingsArray.Where(item => item["key"] != null && item["key"].ToString() == "advantageSetting").ToList();
                    foreach (var item in itemsToRemove)
                    {
                        settingsArray.Remove(item);
                    }
                    JObject newAdvantageSetting = new JObject
                    {
                        ["key"] = "advantageSetting",
                        ["enableadvantage"] = true,
                        ["title"] = null,
                        ["filter"] = null,
                        ["templateValue"] = t2, // 生成新的 GUID
                        ["rptValue"] = "",
                        ["filterSetting"] = null,
                        ["printerFID"] = null,
                        ["selectentity"] = null,
                        ["sortString"] = null,
                        ["sortsqlString"] = null,
                        ["sortConditon"] = null,
                        ["economizePaper"] = false,
                        ["usePaperInfo"] = false,
                        ["groupString"] = null,
                        ["groupSettingName"] = null,
                        ["GroupSortString"] = null
                    }
                ;

                    settingsArray.Add(newAdvantageSetting);

                    // 将修改后的 JArray 转回 JSONArray
                    setting = new JSONArray(settingsArray.ToString());
                }
                PrintServiceHelper.SaveNotePrintPara(this.Context, this.View.BusinessInfo.GetForm().Id, this.Context.UserId, setting.ToJSONString());
            }
            if (tKey.Equals("F_POSW_TAB_T7S_P2_T7S"))
            {
                this.Model.SetValue("FTDMB", 2, 0);
                this.View.UpdateView("FTDMB");
                if (setting != null)
                {
                    // 将 JSONArray 转换为 JArray 以便处理
                    JArray settingsArray = JArray.Parse(setting.ToString());
                    // 1. 删除所有 key 为 "notePrintParameterSetting" 的对象
                    var itemsToRemove2 = settingsArray.Where(item => item["key"] != null && item["key"].ToString() == "notePrintParameterSetting").ToList();
                    foreach (var item in itemsToRemove2)
                    {
                        settingsArray.Remove(item);
                    }
                    //重新添加notePrintParameterSetting
                    JObject newAdvantageSetting2 = new JObject
                    {
                        ["key"] = "notePrintParameterSetting",
                        ["batchPrintPageCount"] = 0,
                        ["showSelectTemplate"] = false,
                        ["listSetPrintNumber"] = false,
                        ["usePaperInfo"] = false,
                        ["paperType"] = "",
                        ["paperOrientation"] = "",
                        ["paperHeight"] = 0,
                        ["paperWidth"] = 0,
                        ["marginTop"] = 0,
                        ["marginBottom"] = 0,
                        ["marginLeft"] = 0,
                        ["marginRight"] = 0,
                        ["singleBillPrint"] = false,
                        ["multTemplatePrintByBill"] = false,
                        ["printFirstFilter"] = false,
                        ["selectEntryFilter"] = false
                    }
                ;

                    settingsArray.Add(newAdvantageSetting2);
                    // 1. 删除所有 key 为 "advantageSetting" 的对象
                    var itemsToRemove = settingsArray.Where(item => item["key"] != null && item["key"].ToString() == "advantageSetting").ToList();
                    foreach (var item in itemsToRemove)
                    {
                        settingsArray.Remove(item);
                    }
                    JObject newAdvantageSetting = new JObject
                    {
                        ["key"] = "advantageSetting",
                        ["enableadvantage"] = true,
                        ["title"] = null,
                        ["filter"] = null,
                        ["templateValue"] = t3, // 生成新的 GUID
                        ["rptValue"] = "",
                        ["filterSetting"] = null,
                        ["printerFID"] = null,
                        ["selectentity"] = null,
                        ["sortString"] = null,
                        ["sortsqlString"] = null,
                        ["sortConditon"] = null,
                        ["economizePaper"] = false,
                        ["usePaperInfo"] = false,
                        ["groupString"] = null,
                        ["groupSettingName"] = null,
                        ["GroupSortString"] = null
                    };

                    settingsArray.Add(newAdvantageSetting);

                    // 将修改后的 JArray 转回 JSONArray
                    setting = new JSONArray(settingsArray.ToString());
                }
                PrintServiceHelper.SaveNotePrintPara(this.Context, this.View.BusinessInfo.GetForm().Id, this.Context.UserId, setting.ToJSONString());
            }
            if (tKey.Equals("F_POSW_TAB_T7S_P_AIK"))
            {
                this.Model.SetValue("FTDMB", 3, 0);
                this.View.UpdateView("FTDMB");

                if (setting != null)
                {
                    // 将 JSONArray 转换为 JArray 以便处理
                    JArray settingsArray = JArray.Parse(setting.ToString());
                    // 1. 删除所有 key 为 "notePrintParameterSetting" 的对象
                    var itemsToRemove2 = settingsArray.Where(item => item["key"] != null && item["key"].ToString() == "notePrintParameterSetting").ToList();
                    foreach (var item in itemsToRemove2)
                    {
                        settingsArray.Remove(item);
                    }
                    //重新添加notePrintParameterSetting
                    JObject newAdvantageSetting2 = new JObject
                    {
                        ["key"] = "notePrintParameterSetting",
                        ["batchPrintPageCount"] = 0,
                        ["showSelectTemplate"] = false,
                        ["listSetPrintNumber"] = false,
                        ["usePaperInfo"] = false,
                        ["paperType"] = "",
                        ["paperOrientation"] = "",
                        ["paperHeight"] = 0,
                        ["paperWidth"] = 0,
                        ["marginTop"] = 0,
                        ["marginBottom"] = 0,
                        ["marginLeft"] = 0,
                        ["marginRight"] = 0,
                        ["singleBillPrint"] = false,
                        ["multTemplatePrintByBill"] = false,
                        ["printFirstFilter"] = false,
                        ["selectEntryFilter"] = false
                    }
                ;

                    settingsArray.Add(newAdvantageSetting2);
                    // 1. 删除所有 key 为 "advantageSetting" 的对象
                    var itemsToRemove = settingsArray.Where(item => item["key"] != null && item["key"].ToString() == "advantageSetting").ToList();
                    foreach (var item in itemsToRemove)
                    {
                        settingsArray.Remove(item);
                    }
                    JObject newAdvantageSetting = new JObject
                    {
                        ["key"] = "advantageSetting",
                        ["enableadvantage"] = true,
                        ["title"] = null,
                        ["filter"] = null,
                        ["templateValue"] = t4, // 生成新的 GUID
                        ["rptValue"] = "",
                        ["filterSetting"] = null,
                        ["printerFID"] = null,
                        ["selectentity"] = null,
                        ["sortString"] = null,
                        ["sortsqlString"] = null,
                        ["sortConditon"] = null,
                        ["economizePaper"] = false,
                        ["usePaperInfo"] = false,
                        ["groupString"] = null,
                        ["groupSettingName"] = null,
                        ["GroupSortString"] = null
                    };

                    settingsArray.Add(newAdvantageSetting);

                    // 将修改后的 JArray 转回 JSONArray
                    setting = new JSONArray(settingsArray.ToString());
                }
                PrintServiceHelper.SaveNotePrintPara(this.Context, this.View.BusinessInfo.GetForm().Id, this.Context.UserId, setting.ToJSONString());
            }
            if (tKey.Equals("F_POSW_TAB_T7S_P_959"))
            {
                this.Model.SetValue("FTDMB", 4, 0);
                this.View.UpdateView("FTDMB");

                if (setting != null)
                {
                    // 将 JSONArray 转换为 JArray 以便处理
                    JArray settingsArray = JArray.Parse(setting.ToString());
                    // 1. 删除所有 key 为 "notePrintParameterSetting" 的对象
                    var itemsToRemove2 = settingsArray.Where(item => item["key"] != null && item["key"].ToString() == "notePrintParameterSetting").ToList();
                    foreach (var item in itemsToRemove2)
                    {
                        settingsArray.Remove(item);
                    }
                    //重新添加notePrintParameterSetting
                    JObject newAdvantageSetting2 = new JObject
                    {
                        ["key"] = "notePrintParameterSetting",
                        ["batchPrintPageCount"] = 0,
                        ["showSelectTemplate"] = false,
                        ["listSetPrintNumber"] = false,
                        ["usePaperInfo"] = false,
                        ["paperType"] = "",
                        ["paperOrientation"] = "",
                        ["paperHeight"] = 0,
                        ["paperWidth"] = 0,
                        ["marginTop"] = 0,
                        ["marginBottom"] = 0,
                        ["marginLeft"] = 0,
                        ["marginRight"] = 0,
                        ["singleBillPrint"] = false,
                        ["multTemplatePrintByBill"] = false,
                        ["printFirstFilter"] = false,
                        ["selectEntryFilter"] = false
                    }
                ;

                    settingsArray.Add(newAdvantageSetting2);
                    // 1. 删除所有 key 为 "advantageSetting" 的对象
                    var itemsToRemove = settingsArray.Where(item => item["key"] != null && item["key"].ToString() == "advantageSetting").ToList();
                    foreach (var item in itemsToRemove)
                    {
                        settingsArray.Remove(item);
                    }
                    JObject newAdvantageSetting = new JObject
                    {
                        ["key"] = "advantageSetting",
                        ["enableadvantage"] = true,
                        ["title"] = null,
                        ["filter"] = null,
                        ["templateValue"] = t5, // 生成新的 GUID
                        ["rptValue"] = "",
                        ["filterSetting"] = null,
                        ["printerFID"] = null,
                        ["selectentity"] = null,
                        ["sortString"] = null,
                        ["sortsqlString"] = null,
                        ["sortConditon"] = null,
                        ["economizePaper"] = false,
                        ["usePaperInfo"] = false,
                        ["groupString"] = null,
                        ["groupSettingName"] = null,
                        ["GroupSortString"] = null
                    };

                    settingsArray.Add(newAdvantageSetting);

                    // 将修改后的 JArray 转回 JSONArray
                    setting = new JSONArray(settingsArray.ToString());
                }
                PrintServiceHelper.SaveNotePrintPara(this.Context, this.View.BusinessInfo.GetForm().Id, this.Context.UserId, setting.ToJSONString());
            }
        }

        private JSONArray GetSetupInfo(string formId)
        {
            string info = PrintServiceHelper.LoadNotePrintPara(this.View.Context, formId, this.View.Context.UserId);
            //将替换过的xml再还原回去.
            info = StringUtils.FixedOrcalXMLString(info);
            return new JSONArray(info);
        }

        public override void EntityRowDoubleClick(EntityRowClickEventArgs e)
        {
            if (BindData()) { return; }
            base.EntityRowDoubleClick(e);
            // 获取双击的行号
            int rowIndex = e.Row;
            // 获取数据包
            var billObj = this.View.Model.DataObject;
            // 获取单据体数据
            DynamicObjectCollection en = billObj["FKHWLZ"] as DynamicObjectCollection;
            // 获取当前双击行的数据
            DynamicObject currentRowObj = en[rowIndex];
            var bomV = currentRowObj["FCUSTID"] as DynamicObject;
            long custId = bomV == null ? 0 : Convert.ToInt64(bomV["id"]);
            //string strDate = DateTime.Now.ToString("yyyy-MM-dd");
            //string endDate = DateTime.Now.ToString("yyyy-MM-dd");
            string strDate = Convert.ToDateTime(this.Model.DataObject["FSTADATE"]).ToString("yyyy-MM-dd");
            string endDate = Convert.ToDateTime(this.Model.DataObject["FENDDATE"]).ToString("yyyy-MM-dd");
            this.View.Model.ClearNoDataRow();
            if (!custId.IsNullOrEmptyOrWhiteSpace())
            {
                string custName = bomV == null ? "" : bomV["name"].ToString();
                string xsyName = bomV == null ? "" : bomV["F_QNXN_Text_XSY"].ToString();
                this.Model.SetItemValueByID("F_POSW_Base_xvz", custId, 0);
                this.Model.SetValue("FCustName", custName, 0);
                this.View.UpdateView("F_POSW_Base_xvz");
                this.View.UpdateView("FCustName");

                this.Model.SetValue("FXSYNAME", xsyName, 0);
                this.View.UpdateView("FXSYNAME");
                //F_POSW_CheckBox_c6t
                bool isFree = Convert.ToBoolean(this.Model.DataObject["F_POSW_CheckBox_c6t"]);
                //对账单(副表) 2
                DynamicObjectCollection t2Datas = GetT2Datas(strDate, endDate, custId);
                if (t2Datas.Count > 0)
                {
                    this.Model.DeleteEntryData("F_POSW_Entity_mpw");
                    this.Model.BatchCreateNewEntryRow("F_POSW_Entity_mpw", t2Datas.Count);
                    for (int i = 0; i < t2Datas.Count; i++)
                    {
                        this.Model.SetValue("FSEQ", t2Datas[i]["FSEQ"], i);
                        this.Model.SetValue("F_POSW_Date_1bv", t2Datas[i]["FDATE"], i);
                        this.Model.SetValue("F_POSW_Text_0ne", t2Datas[i]["FBILLNO"], i);
                        this.Model.SetValue("F_POSW_Text_uo3", t2Datas[i]["FZY"], i);
                        this.Model.SetValue("F_POSW_Amount_rec", t2Datas[i]["FXSJE"], i);
                        this.Model.SetValue("F_POSW_Amount_bpq", t2Datas[i]["FHKJE"], i);
                        this.Model.SetValue("F_POSW_Amount_d7o", t2Datas[i]["FQMQK"], i);
                        this.Model.SetValue("FCPQty", t2Datas[i]["FCPQty"], i);
                        this.Model.SetValue("FDZQty", t2Datas[i]["FDZQty"], i);
                        this.Model.SetValue("FYearMonth", t2Datas[i]["FYearMonth"], i);
                    }
                    this.View.Model.ClearNoDataRow();
                    this.View.UpdateView("F_POSW_Entity_mpw");
                }
                //明细对账单（副表） 3
                DynamicObjectCollection t3Datas = GetT3Datas(strDate, endDate, custId);
                if (t3Datas.Count > 0)
                {
                    this.Model.DeleteEntryData("F_POSW_Entity_60c");
                    this.Model.BatchCreateNewEntryRow("F_POSW_Entity_60c", t3Datas.Count);
                    for (int i = 0; i < t3Datas.Count; i++)
                    {
                        this.Model.SetValue("FSEQ2", t3Datas[i]["FSEQ"], i);
                        this.Model.SetValue("F_POSW_Date_e9h", t3Datas[i]["FDATE"], i);
                        this.Model.SetValue("F_POSW_Text_zg7", t3Datas[i]["FBILLNO"], i);
                        this.Model.SetValue("F_POSW_Text_9l8", t3Datas[i]["FZY"], i);
                        this.Model.SetValue("FDW", t3Datas[i]["FDW"], i);
                        this.Model.SetValue("F_POSW_Qty_xxu", t3Datas[i]["FSL"], i);
                        this.Model.SetValue("F_POSW_Decimal_ikt", t3Datas[i]["FZL"], i);
                        this.Model.SetValue("F_POSW_Price_xfa", t3Datas[i]["FKPJ"], i);
                        this.Model.SetValue("F_POSW_Decimal_f5d", t3Datas[i]["FZK"], i);
                        this.Model.SetValue("F_POSW_Price_vzn", t3Datas[i]["FXSJ"], i);
                        this.Model.SetValue("F_POSW_Amount_y10", t3Datas[i]["FXSJE"], i);
                        this.Model.SetValue("F_POSW_Amount_ito", t3Datas[i]["FHKJE"], i);
                        this.Model.SetValue("F_POSW_Amount_vxl", t3Datas[i]["FQMQK"], i);
                    }
                    this.View.Model.ClearNoDataRow();
                    this.View.UpdateView("F_POSW_Entity_60c");
                }
                //客户品种销售（副表） 4
                DynamicObjectCollection t4Datas = GetT4Datas(strDate, endDate, custId, isFree);
                if (t4Datas.Count > 0)
                {
                    this.Model.DeleteEntryData("F_POSW_Entity_vbc");
                    this.Model.BatchCreateNewEntryRow("F_POSW_Entity_vbc", t4Datas.Count);
                    for (int i = 0; i < t4Datas.Count; i++)
                    {
                        //this.Model.SetItemValueByNumber("F_POSW_Base_aec", t4Datas[i]["FPZBH"].ToString(), i);
                        this.Model.SetValue("FPZBH", t4Datas[i]["FPZBH"], i);
                        this.Model.SetValue("F_POSW_Text_n6p", t4Datas[i]["FWLMC"], i);
                        this.Model.SetValue("F_POSW_Decimal_i6k", t4Datas[i]["FKG"], i);
                        this.Model.SetValue("F_POSW_Decimal_amv", t4Datas[i]["FBAO"], i);
                        this.Model.SetValue("F_POSW_Decimal_7wd", t4Datas[i]["FTUN"], i);
                        this.Model.SetValue("F_POSW_Amount_gmk", t4Datas[i]["FSJJE"], i);
                    }
                    this.View.Model.ClearNoDataRow();
                    this.View.UpdateView("F_POSW_Entity_vbc");
                }
                //销售分类（副表） 5
                DynamicObjectCollection t5Datas = GetT5Datas(strDate, endDate, custId, isFree);
                if (t5Datas.Count > 0)
                {
                    this.Model.DeleteEntryData("F_POSW_Entity_6yb");
                    this.Model.BatchCreateNewEntryRow("F_POSW_Entity_6yb", t5Datas.Count);
                    for (int i = 0; i < t5Datas.Count; i++)
                    {
                        this.Model.SetValue("F_POSW_Text_ii9", t5Datas[i]["FXSFZ"], i);
                        this.Model.SetValue("F_POSW_Decimal_rks", t5Datas[i]["FKG"], i);
                        this.Model.SetValue("F_POSW_Decimal_71r", t5Datas[i]["FBAO"], i);
                        this.Model.SetValue("F_POSW_Decimal_f02", t5Datas[i]["FTUN"], i);
                        this.Model.SetValue("F_POSW_Amount_15v", t5Datas[i]["FSJJE"], i);
                    }
                    this.View.Model.ClearNoDataRow();
                    this.View.UpdateView("F_POSW_Entity_6yb");
                }
            }
        }
        public override void ButtonClick(ButtonClickEventArgs e)
        {
            if (BindData()) { return; }
            base.ButtonClick(e);
            //过滤主表
            if (e.Key.Equals("FKJGL"))
            {
                var obj = this.Model.DataObject;
                if (obj == null) { return; }
                string filterKey = obj["FXLTJ"] == null ? "" : obj["FXLTJ"].ToString();
                string bjf = obj["FBJF"] == null ? "" : obj["FBJF"].ToString();
                decimal bjz = obj["FBJZ"] == null ? 0 : Convert.ToDecimal(obj["FBJZ"]);
                if (filterKey.IsNullOrEmptyOrWhiteSpace() || bjf.IsNullOrEmptyOrWhiteSpace())
                {
                    this.View.ShowErrMessage("过滤字段和过滤比较符不能为空，请确认！");
                    return;
                }
                var oldRows = this.Model.DataObject["FKHWLZ"] as DynamicObjectCollection;
                if (oldRows.Count <= 0) { return; }
                //大于等于
                if (bjf == "0")
                {
                    var newRows = oldRows.Where(w => Convert.ToDecimal(w[filterKey]) >= bjz).ToList();
                    this.Model.DeleteEntryData("FKHWLZ");
                    this.Model.BatchCreateNewEntryRow("FKHWLZ", newRows.Count);
                    for (int i = 0; i < newRows.Count; i++)
                    {
                        this.Model.SetValue("FCPLB", newRows[i]["FCPLB"], i);
                        this.Model.SetValue("FXSTD", newRows[i]["FXSTD"], i);
                        this.Model.SetValue("FXSBM", newRows[i]["FXSBM"], i);
                        this.Model.SetValue("FXSY", newRows[i]["FXSY"], i);
                        this.Model.SetValue("FNUMBER", newRows[i]["FNUMBER"], i);
                        this.Model.SetValue("FNAME", newRows[i]["FNAME"], i);
                        this.Model.SetValue("FQCYE", newRows[i]["FQCYE"], i);
                        this.Model.SetValue("FBQXL", newRows[i]["FBQXL"], i);
                        this.Model.SetValue("FBQQTXL", newRows[i]["FBQQTXL"], i);
                        this.Model.SetValue("FBQQTXE", newRows[i]["FBQQTXE"], i);
                        this.Model.SetValue("FBQXE", newRows[i]["FBQXE"], i);
                        this.Model.SetValue("FBQSKE", newRows[i]["FBQSKE"], i);
                        this.Model.SetValue("FBQYE", newRows[i]["FBQYE"], i);
                        this.Model.SetValue("FDQZQK", newRows[i]["FDQZQK"], i);
                        this.Model.SetItemValueByID("FCUSTID", newRows[i]["FCUSTID"], i);
                    }
                    this.View.Model.ClearNoDataRow();
                    this.View.UpdateView("FKHWLZ");
                }
                //大于
                if (bjf == "1")
                {
                    var newRows = oldRows.Where(w => Convert.ToDecimal(w[filterKey]) > bjz).ToList();
                    this.Model.DeleteEntryData("FKHWLZ");
                    this.Model.BatchCreateNewEntryRow("FKHWLZ", newRows.Count);
                    for (int i = 0; i < newRows.Count; i++)
                    {
                        this.Model.SetValue("FCPLB", newRows[i]["FCPLB"], i);
                        this.Model.SetValue("FXSTD", newRows[i]["FXSTD"], i);
                        this.Model.SetValue("FXSBM", newRows[i]["FXSBM"], i);
                        this.Model.SetValue("FXSY", newRows[i]["FXSY"], i);
                        this.Model.SetValue("FNUMBER", newRows[i]["FNUMBER"], i);
                        this.Model.SetValue("FNAME", newRows[i]["FNAME"], i);
                        this.Model.SetValue("FQCYE", newRows[i]["FQCYE"], i);
                        this.Model.SetValue("FBQXL", newRows[i]["FBQXL"], i);
                        this.Model.SetValue("FBQQTXL", newRows[i]["FBQQTXL"], i);
                        this.Model.SetValue("FBQQTXE", newRows[i]["FBQQTXE"], i);
                        this.Model.SetValue("FBQXE", newRows[i]["FBQXE"], i);
                        this.Model.SetValue("FBQSKE", newRows[i]["FBQSKE"], i);
                        this.Model.SetValue("FBQYE", newRows[i]["FBQYE"], i);
                        this.Model.SetValue("FDQZQK", newRows[i]["FDQZQK"], i);
                        this.Model.SetItemValueByID("FCUSTID", newRows[i]["FCUSTID"], i);
                    }
                    this.View.Model.ClearNoDataRow();
                    this.View.UpdateView("FKHWLZ");
                }
                //等于
                if (bjf == "2")
                {
                    var newRows = oldRows.Where(w => Convert.ToDecimal(w[filterKey]) == bjz).ToList();
                    this.Model.DeleteEntryData("FKHWLZ");
                    this.Model.BatchCreateNewEntryRow("FKHWLZ", newRows.Count);
                    for (int i = 0; i < newRows.Count; i++)
                    {
                        this.Model.SetValue("FCPLB", newRows[i]["FCPLB"], i);
                        this.Model.SetValue("FXSTD", newRows[i]["FXSTD"], i);
                        this.Model.SetValue("FXSBM", newRows[i]["FXSBM"], i);
                        this.Model.SetValue("FXSY", newRows[i]["FXSY"], i);
                        this.Model.SetValue("FNUMBER", newRows[i]["FNUMBER"], i);
                        this.Model.SetValue("FNAME", newRows[i]["FNAME"], i);
                        this.Model.SetValue("FQCYE", newRows[i]["FQCYE"], i);
                        this.Model.SetValue("FBQXL", newRows[i]["FBQXL"], i);
                        this.Model.SetValue("FBQQTXL", newRows[i]["FBQQTXL"], i);
                        this.Model.SetValue("FBQQTXE", newRows[i]["FBQQTXE"], i);
                        this.Model.SetValue("FBQXE", newRows[i]["FBQXE"], i);
                        this.Model.SetValue("FBQSKE", newRows[i]["FBQSKE"], i);
                        this.Model.SetValue("FBQYE", newRows[i]["FBQYE"], i);
                        this.Model.SetValue("FDQZQK", newRows[i]["FDQZQK"], i);
                        this.Model.SetItemValueByID("FCUSTID", newRows[i]["FCUSTID"], i);
                    }
                    this.View.Model.ClearNoDataRow();
                    this.View.UpdateView("FKHWLZ");
                }
                //小于
                if (bjf == "3")
                {
                    var newRows = oldRows.Where(w => Convert.ToDecimal(w[filterKey]) < bjz).ToList();
                    this.Model.DeleteEntryData("FKHWLZ");
                    this.Model.BatchCreateNewEntryRow("FKHWLZ", newRows.Count);
                    for (int i = 0; i < newRows.Count; i++)
                    {
                        this.Model.SetValue("FCPLB", newRows[i]["FCPLB"], i);
                        this.Model.SetValue("FXSTD", newRows[i]["FXSTD"], i);
                        this.Model.SetValue("FXSBM", newRows[i]["FXSBM"], i);
                        this.Model.SetValue("FXSY", newRows[i]["FXSY"], i);
                        this.Model.SetValue("FNUMBER", newRows[i]["FNUMBER"], i);
                        this.Model.SetValue("FNAME", newRows[i]["FNAME"], i);
                        this.Model.SetValue("FQCYE", newRows[i]["FQCYE"], i);
                        this.Model.SetValue("FBQXL", newRows[i]["FBQXL"], i);
                        this.Model.SetValue("FBQQTXL", newRows[i]["FBQQTXL"], i);
                        this.Model.SetValue("FBQQTXE", newRows[i]["FBQQTXE"], i);
                        this.Model.SetValue("FBQXE", newRows[i]["FBQXE"], i);
                        this.Model.SetValue("FBQSKE", newRows[i]["FBQSKE"], i);
                        this.Model.SetValue("FBQYE", newRows[i]["FBQYE"], i);
                        this.Model.SetValue("FDQZQK", newRows[i]["FDQZQK"], i);
                        this.Model.SetItemValueByID("FCUSTID", newRows[i]["FCUSTID"], i);
                    }
                    this.View.Model.ClearNoDataRow();
                    this.View.UpdateView("FKHWLZ");
                }
                //大于等于
                if (bjf == "4")
                {
                    var newRows = oldRows.Where(w => Convert.ToDecimal(w[filterKey]) <= bjz).ToList();
                    this.Model.DeleteEntryData("FKHWLZ");
                    this.Model.BatchCreateNewEntryRow("FKHWLZ", newRows.Count);
                    for (int i = 0; i < newRows.Count; i++)
                    {
                        this.Model.SetValue("FCPLB", newRows[i]["FCPLB"], i);
                        this.Model.SetValue("FXSTD", newRows[i]["FXSTD"], i);
                        this.Model.SetValue("FXSBM", newRows[i]["FXSBM"], i);
                        this.Model.SetValue("FXSY", newRows[i]["FXSY"], i);
                        this.Model.SetValue("FNUMBER", newRows[i]["FNUMBER"], i);
                        this.Model.SetValue("FNAME", newRows[i]["FNAME"], i);
                        this.Model.SetValue("FQCYE", newRows[i]["FQCYE"], i);
                        this.Model.SetValue("FBQXL", newRows[i]["FBQXL"], i);
                        this.Model.SetValue("FBQQTXL", newRows[i]["FBQQTXL"], i);
                        this.Model.SetValue("FBQQTXE", newRows[i]["FBQQTXE"], i);
                        this.Model.SetValue("FBQXE", newRows[i]["FBQXE"], i);
                        this.Model.SetValue("FBQSKE", newRows[i]["FBQSKE"], i);
                        this.Model.SetValue("FBQYE", newRows[i]["FBQYE"], i);
                        this.Model.SetValue("FDQZQK", newRows[i]["FDQZQK"], i);
                        this.Model.SetItemValueByID("FCUSTID", newRows[i]["FCUSTID"], i);
                    }
                    this.View.Model.ClearNoDataRow();
                    this.View.UpdateView("FKHWLZ");
                }
            }
            //刷新
            if (e.Key.Equals("F_POSW_BUTTON_7AQ"))
            {
                var obj = this.Model.DataObject;
                object res = this.Model.OpenParameter.GetCustomParameter("CustId");
                if (obj == null) { return; }
                DateTime strDate = Convert.ToDateTime(this.Model.DataObject["FSTADATE"]);
                DateTime endDate = Convert.ToDateTime(this.Model.DataObject["FENDDATE"]);
                var cust = obj["F_POSW_Base_xvz"] as DynamicObject;
                var depList = obj["FDEP"] as DynamicObjectCollection;
                string depStr = "";
                if (depList.Count > 0 && depList != null)
                {
                    List<string> dlList = new List<string>();
                    foreach (var d in depList)
                    {
                        var dObj = d["FDEP"] as DynamicObject;
                        string dName = dObj["name"].ToString();
                        dlList.Add(dName);
                    }
                    // 转义单引号并用单引号包裹每个值
                    var escapedValues = dlList.Select(s => $"'{s.Replace("'", "''")}'");
                    string cutids = string.Join(",", escapedValues);
                    //filStr += $" AND T4.FNAME IN ({cutids})";
                    depStr += $" where t5.FNAME in ({cutids}) ";
                }
                this.Model.DeleteEntryData("FKHWLZ");
                if (cust == null)
                {
                    this.Model.DeleteEntryData("F_POSW_Entity_mpw");
                    this.View.UpdateView("F_POSW_Entity_mpw");

                    this.Model.DeleteEntryData("F_POSW_Entity_60c");
                    this.View.UpdateView("F_POSW_Entity_60c");

                    this.Model.DeleteEntryData("F_POSW_Entity_vbc");
                    this.View.UpdateView("F_POSW_Entity_vbc");

                    this.Model.DeleteEntryData("F_POSW_Entity_6yb");
                    this.View.UpdateView("F_POSW_Entity_6yb");

                    var grid = this.View.GetControl<EntryGrid>("FKHWLZ");
                    grid.Enabled = false;
                    var backColor1 = "#CCCFCC";
                    var backColor2 = "#F9F8BE";

                    DynamicObjectCollection allDatas = GetDatas(strDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"), depStr);
                    if (allDatas.Count > 0)
                    {
                        this.Model.BatchCreateNewEntryRow("FKHWLZ", allDatas.Count);
                        for (int i = 0; i < allDatas.Count; i++)
                        {
                            this.Model.SetValue("FCPLB", allDatas[i]["FCPLB"], i);
                            this.Model.SetValue("FXSTD", allDatas[i]["FXSTD"], i);
                            this.Model.SetValue("FXSBM", allDatas[i]["FXSBM"], i);
                            this.Model.SetValue("FXSY", allDatas[i]["FXSY"], i);
                            this.Model.SetValue("FNUMBER", allDatas[i]["FNUMBER"], i);
                            this.Model.SetValue("FNAME", allDatas[i]["FNAME"], i);
                            this.Model.SetValue("FQCYE", allDatas[i]["FQCYE"], i);
                            this.Model.SetValue("FBQXL", allDatas[i]["FBQXL"], i);
                            this.Model.SetValue("FBQQTXL", allDatas[i]["FBQQTXL"], i);
                            this.Model.SetValue("FBQQTXE", allDatas[i]["FBQQTXE"], i);
                            this.Model.SetValue("FBQXE", allDatas[i]["FBQXE"], i);
                            this.Model.SetValue("FBQSKE", allDatas[i]["FBQSKE"], i);
                            this.Model.SetValue("FBQYE", allDatas[i]["FBQYE"], i);
                            this.Model.SetValue("FDQZQK", allDatas[i]["FDQZQK"], i);
                            this.Model.SetItemValueByID("FCUSTID", allDatas[i]["FCUSTID"], i);
                        }
                        this.View.Model.ClearNoDataRow();
                        this.View.UpdateView("FKHWLZ");
                        for (int i = 0; i < allDatas.Count; i++)
                        {
                            if (i % 2 == 1)
                            {
                                grid.SetRowBackcolor(backColor1, i);
                                //Logger.Info("FKHWLZ", $"Setting row {i} background color:{backColor1}");
                            }
                            else
                            {
                                grid.SetRowBackcolor(backColor2, i);
                                //Logger.Info("FKHWLZ", $"Setting row {i} background color:{backColor2}");
                            }
                        }
                    }
                }
                else
                {
                    bool isFree = Convert.ToBoolean(this.Model.DataObject["F_POSW_CheckBox_c6t"]);
                    long custId = cust == null ? 0 : Convert.ToInt64(cust["id"]);
                    DynamicObjectCollection allDatas = GetDatas2(strDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"), custId);
                    if (allDatas.Count > 0)
                    {
                        this.Model.DeleteEntryData("FKHWLZ");
                        this.Model.BatchCreateNewEntryRow("FKHWLZ", allDatas.Count);
                        for (int i = 0; i < allDatas.Count; i++)
                        {
                            this.Model.SetValue("FCPLB", allDatas[i]["FCPLB"], i);
                            this.Model.SetValue("FXSTD", allDatas[i]["FXSTD"], i);
                            this.Model.SetValue("FXSBM", allDatas[i]["FXSBM"], i);
                            this.Model.SetValue("FXSY", allDatas[i]["FXSY"], i);
                            this.Model.SetValue("FNUMBER", allDatas[i]["FNUMBER"], i);
                            this.Model.SetValue("FNAME", allDatas[i]["FNAME"], i);
                            this.Model.SetValue("FQCYE", allDatas[i]["FQCYE"], i);
                            this.Model.SetValue("FBQXL", allDatas[i]["FBQXL"], i);
                            this.Model.SetValue("FBQQTXL", allDatas[i]["FBQQTXL"], i);
                            this.Model.SetValue("FBQQTXE", allDatas[i]["FBQQTXE"], i);
                            this.Model.SetValue("FBQXE", allDatas[i]["FBQXE"], i);
                            this.Model.SetValue("FBQSKE", allDatas[i]["FBQSKE"], i);
                            this.Model.SetValue("FBQYE", allDatas[i]["FBQYE"], i);
                            this.Model.SetValue("FDQZQK", allDatas[i]["FDQZQK"], i);
                            this.Model.SetItemValueByID("FCUSTID", allDatas[i]["FCUSTID"], i);
                        }
                        this.View.Model.ClearNoDataRow();
                        this.View.UpdateView("FKHWLZ");
                    }
                    //对账单(副表) 2
                    DynamicObjectCollection t2Datas = GetT2Datas(strDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"), custId);
                    if (t2Datas.Count > 0)
                    {
                        this.Model.DeleteEntryData("F_POSW_Entity_mpw");
                        this.Model.BatchCreateNewEntryRow("F_POSW_Entity_mpw", t2Datas.Count);
                        for (int i = 0; i < t2Datas.Count; i++)
                        {
                            this.Model.SetValue("FSEQ", t2Datas[i]["FSEQ"], i);
                            this.Model.SetValue("F_POSW_Date_1bv", t2Datas[i]["FDATE"], i);
                            this.Model.SetValue("F_POSW_Text_0ne", t2Datas[i]["FBILLNO"], i);
                            this.Model.SetValue("F_POSW_Text_uo3", t2Datas[i]["FZY"], i);
                            this.Model.SetValue("F_POSW_Amount_rec", t2Datas[i]["FXSJE"], i);
                            this.Model.SetValue("F_POSW_Amount_bpq", t2Datas[i]["FHKJE"], i);
                            this.Model.SetValue("F_POSW_Amount_d7o", t2Datas[i]["FQMQK"], i);
                            this.Model.SetValue("FCPQty", t2Datas[i]["FCPQty"], i);
                            this.Model.SetValue("FDZQty", t2Datas[i]["FDZQty"], i);
                            this.Model.SetValue("FYearMonth", t2Datas[i]["FYearMonth"], i);
                        }
                        this.View.Model.ClearNoDataRow();
                        this.View.UpdateView("F_POSW_Entity_mpw");
                    }
                    //明细对账单（副表） 3
                    DynamicObjectCollection t3Datas = GetT3Datas(strDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"), custId);
                    if (t3Datas.Count > 0)
                    {
                        this.Model.DeleteEntryData("F_POSW_Entity_60c");
                        this.Model.BatchCreateNewEntryRow("F_POSW_Entity_60c", t3Datas.Count);
                        for (int i = 0; i < t3Datas.Count; i++)
                        {
                            this.Model.SetValue("FSEQ2", t3Datas[i]["FSEQ"], i);
                            this.Model.SetValue("F_POSW_Date_e9h", t3Datas[i]["FDATE"], i);
                            this.Model.SetValue("F_POSW_Text_zg7", t3Datas[i]["FBILLNO"], i);
                            this.Model.SetValue("F_POSW_Text_9l8", t3Datas[i]["FZY"], i);
                            this.Model.SetValue("FDW", t3Datas[i]["FDW"], i);
                            this.Model.SetValue("F_POSW_Qty_xxu", t3Datas[i]["FSL"], i);
                            this.Model.SetValue("F_POSW_Decimal_ikt", t3Datas[i]["FZL"], i);
                            this.Model.SetValue("F_POSW_Price_xfa", t3Datas[i]["FKPJ"], i);
                            this.Model.SetValue("F_POSW_Decimal_f5d", t3Datas[i]["FZK"], i);
                            this.Model.SetValue("F_POSW_Price_vzn", t3Datas[i]["FXSJ"], i);
                            this.Model.SetValue("F_POSW_Amount_y10", t3Datas[i]["FXSJE"], i);
                            this.Model.SetValue("F_POSW_Amount_ito", t3Datas[i]["FHKJE"], i);
                            this.Model.SetValue("F_POSW_Amount_vxl", t3Datas[i]["FQMQK"], i);
                        }
                        this.View.Model.ClearNoDataRow();
                        this.View.UpdateView("F_POSW_Entity_60c");
                    }
                    //客户品种销售（副表） 4
                    DynamicObjectCollection t4Datas = GetT4Datas(strDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"), custId, isFree);
                    if (t4Datas.Count > 0)
                    {
                        this.Model.DeleteEntryData("F_POSW_Entity_vbc");
                        this.Model.BatchCreateNewEntryRow("F_POSW_Entity_vbc", t4Datas.Count);
                        for (int i = 0; i < t4Datas.Count; i++)
                        {
                            //this.Model.SetItemValueByNumber("F_POSW_Base_aec", t4Datas[i]["FPZBH"].ToString(), i);
                            this.Model.SetValue("FPZBH", t4Datas[i]["FPZBH"], i);
                            this.Model.SetValue("F_POSW_Text_n6p", t4Datas[i]["FWLMC"], i);
                            this.Model.SetValue("F_POSW_Decimal_i6k", t4Datas[i]["FKG"], i);
                            this.Model.SetValue("F_POSW_Decimal_amv", t4Datas[i]["FBAO"], i);
                            this.Model.SetValue("F_POSW_Decimal_7wd", t4Datas[i]["FTUN"], i);
                            this.Model.SetValue("F_POSW_Amount_gmk", t4Datas[i]["FSJJE"], i);
                        }
                        this.View.Model.ClearNoDataRow();
                        this.View.UpdateView("F_POSW_Entity_vbc");
                    }
                    //销售分类（副表） 5
                    DynamicObjectCollection t5Datas = GetT5Datas(strDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"), custId, isFree);
                    if (t5Datas.Count > 0)
                    {
                        this.Model.DeleteEntryData("F_POSW_Entity_6yb");
                        this.Model.BatchCreateNewEntryRow("F_POSW_Entity_6yb", t5Datas.Count);
                        for (int i = 0; i < t5Datas.Count; i++)
                        {
                            this.Model.SetValue("F_POSW_Text_ii9", t5Datas[i]["FXSFZ"], i);
                            this.Model.SetValue("F_POSW_Decimal_rks", t5Datas[i]["FKG"], i);
                            this.Model.SetValue("F_POSW_Decimal_71r", t5Datas[i]["FBAO"], i);
                            this.Model.SetValue("F_POSW_Decimal_f02", t5Datas[i]["FTUN"], i);
                            this.Model.SetValue("F_POSW_Amount_15v", t5Datas[i]["FSJJE"], i);
                        }
                        this.View.Model.ClearNoDataRow();
                        this.View.UpdateView("F_POSW_Entity_6yb");
                    }
                }

            }
            //昨天
            if (e.Key.Equals("FZT"))
            {
                string strDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                string endDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                this.Model.SetValue("FSTADATE", strDate, 0);
                this.Model.SetValue("FENDDATE", endDate, 0);
                this.Model.DeleteEntryData("FKHWLZ");
                this.View.UpdateView("Kb61c946ccfd787f9");
                //DynamicObjectCollection allDatas = GetDatas(strDate, endDate);
                //if (allDatas.Count > 0)
                //{
                //    this.Model.BatchCreateNewEntryRow("FKHWLZ", allDatas.Count);
                //    //var rows = this.Model.DataObject["FKHWLZ"] as DynamicObjectCollection;
                //    for (int i = 0; i < allDatas.Count; i++)
                //    {
                //        this.Model.SetValue("FCPLB", allDatas[i]["FCPLB"], i);
                //        this.Model.SetValue("FXSTD", allDatas[i]["FXSTD"], i);
                //        this.Model.SetValue("FXSY", allDatas[i]["FXSY"], i);
                //        this.Model.SetValue("FNUMBER", allDatas[i]["FNUMBER"], i);
                //        this.Model.SetValue("FNAME", allDatas[i]["FNAME"], i);
                //        this.Model.SetValue("FQCYE", allDatas[i]["FQCYE"], i);
                //        this.Model.SetValue("FBQXL", allDatas[i]["FBQXL"], i);
                //        this.Model.SetValue("FBQQTXL", allDatas[i]["FBQQTXL"], i);
                //        this.Model.SetValue("FBQQTXE", allDatas[i]["FBQQTXE"], i);
                //        this.Model.SetValue("FBQXE", allDatas[i]["FBQXE"], i);
                //        this.Model.SetValue("FBQSKE", allDatas[i]["FBQSKE"], i);
                //        this.Model.SetValue("FBQYE", allDatas[i]["FBQYE"], i);
                //        this.Model.SetValue("FDQZQK", allDatas[i]["FDQZQK"], i);
                //    }
                //    this.View.UpdateView("FKHWLZ");
                //}
                //this.View.UpdateView();
            }
            //今天
            if (e.Key.Equals("FJT"))
            {
                string strDate = DateTime.Now.ToString("yyyy-MM-dd");
                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                this.Model.SetValue("FSTADATE", strDate, 0);
                this.Model.SetValue("FENDDATE", endDate, 0);
                this.Model.DeleteEntryData("FKHWLZ");
                this.View.UpdateView("Kb61c946ccfd787f9");
                //DynamicObjectCollection allDatas = GetDatas(strDate, endDate);
                //if (allDatas.Count > 0)
                //{
                //    this.Model.BatchCreateNewEntryRow("FKHWLZ", allDatas.Count);
                //    //var rows = this.Model.DataObject["FKHWLZ"] as DynamicObjectCollection;
                //    for (int i = 0; i < allDatas.Count; i++)
                //    {
                //        this.Model.SetValue("FCPLB", allDatas[i]["FCPLB"], i);
                //        this.Model.SetValue("FXSTD", allDatas[i]["FXSTD"], i);
                //        this.Model.SetValue("FXSY", allDatas[i]["FXSY"], i);
                //        this.Model.SetValue("FNUMBER", allDatas[i]["FNUMBER"], i);
                //        this.Model.SetValue("FNAME", allDatas[i]["FNAME"], i);
                //        this.Model.SetValue("FQCYE", allDatas[i]["FQCYE"], i);
                //        this.Model.SetValue("FBQXL", allDatas[i]["FBQXL"], i);
                //        this.Model.SetValue("FBQQTXL", allDatas[i]["FBQQTXL"], i);
                //        this.Model.SetValue("FBQQTXE", allDatas[i]["FBQQTXE"], i);
                //        this.Model.SetValue("FBQXE", allDatas[i]["FBQXE"], i);
                //        this.Model.SetValue("FBQSKE", allDatas[i]["FBQSKE"], i);
                //        this.Model.SetValue("FBQYE", allDatas[i]["FBQYE"], i);
                //        this.Model.SetValue("FDQZQK", allDatas[i]["FDQZQK"], i);
                //    }
                //    this.View.UpdateView("FKHWLZ");
                //}
                //this.View.UpdateView();
            }
            //明天
            if (e.Key.Equals("FMT"))
            {
                string strDate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
                string endDate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
                this.Model.SetValue("FSTADATE", strDate, 0);
                this.Model.SetValue("FENDDATE", endDate, 0);
                this.Model.DeleteEntryData("FKHWLZ");
                this.View.UpdateView("Kb61c946ccfd787f9");
                //DynamicObjectCollection allDatas = GetDatas(strDate, endDate);
                //if (allDatas.Count > 0)
                //{
                //    this.Model.BatchCreateNewEntryRow("FKHWLZ", allDatas.Count);
                //    //var rows = this.Model.DataObject["FKHWLZ"] as DynamicObjectCollection;
                //    for (int i = 0; i < allDatas.Count; i++)
                //    {
                //        this.Model.SetValue("FCPLB", allDatas[i]["FCPLB"], i);
                //        this.Model.SetValue("FXSTD", allDatas[i]["FXSTD"], i);
                //        this.Model.SetValue("FXSY", allDatas[i]["FXSY"], i);
                //        this.Model.SetValue("FNUMBER", allDatas[i]["FNUMBER"], i);
                //        this.Model.SetValue("FNAME", allDatas[i]["FNAME"], i);
                //        this.Model.SetValue("FQCYE", allDatas[i]["FQCYE"], i);
                //        this.Model.SetValue("FBQXL", allDatas[i]["FBQXL"], i);
                //        this.Model.SetValue("FBQQTXL", allDatas[i]["FBQQTXL"], i);
                //        this.Model.SetValue("FBQQTXE", allDatas[i]["FBQQTXE"], i);
                //        this.Model.SetValue("FBQXE", allDatas[i]["FBQXE"], i);
                //        this.Model.SetValue("FBQSKE", allDatas[i]["FBQSKE"], i);
                //        this.Model.SetValue("FBQYE", allDatas[i]["FBQYE"], i);
                //        this.Model.SetValue("FDQZQK", allDatas[i]["FDQZQK"], i);
                //    }
                //    this.View.UpdateView("FKHWLZ");
                //}
                //this.View.UpdateView();
            }
            //前一天
            if (e.Key.Equals("FQYT"))
            {
                DateTime strDate = Convert.ToDateTime(this.Model.DataObject["FSTADATE"]);
                DateTime endDate = Convert.ToDateTime(this.Model.DataObject["FENDDATE"]);
                this.Model.SetValue("FSTADATE", strDate.AddDays(1), 0);
                this.Model.SetValue("FENDDATE", endDate.AddDays(1), 0);
                this.Model.DeleteEntryData("FKHWLZ");
                this.View.UpdateView("Kb61c946ccfd787f9");
                //DynamicObjectCollection allDatas = GetDatas(strDate, endDate);
                //if (allDatas.Count > 0)
                //{
                //    this.Model.BatchCreateNewEntryRow("FKHWLZ", allDatas.Count);
                //    //var rows = this.Model.DataObject["FKHWLZ"] as DynamicObjectCollection;
                //    for (int i = 0; i < allDatas.Count; i++)
                //    {
                //        this.Model.SetValue("FCPLB", allDatas[i]["FCPLB"], i);
                //        this.Model.SetValue("FXSTD", allDatas[i]["FXSTD"], i);
                //        this.Model.SetValue("FXSY", allDatas[i]["FXSY"], i);
                //        this.Model.SetValue("FNUMBER", allDatas[i]["FNUMBER"], i);
                //        this.Model.SetValue("FNAME", allDatas[i]["FNAME"], i);
                //        this.Model.SetValue("FQCYE", allDatas[i]["FQCYE"], i);
                //        this.Model.SetValue("FBQXL", allDatas[i]["FBQXL"], i);
                //        this.Model.SetValue("FBQQTXL", allDatas[i]["FBQQTXL"], i);
                //        this.Model.SetValue("FBQQTXE", allDatas[i]["FBQQTXE"], i);
                //        this.Model.SetValue("FBQXE", allDatas[i]["FBQXE"], i);
                //        this.Model.SetValue("FBQSKE", allDatas[i]["FBQSKE"], i);
                //        this.Model.SetValue("FBQYE", allDatas[i]["FBQYE"], i);
                //        this.Model.SetValue("FDQZQK", allDatas[i]["FDQZQK"], i);
                //    }
                //    this.View.UpdateView("FKHWLZ");
                //}
                //this.View.UpdateView();
            }
            //后一天
            if (e.Key.Equals("FHYT"))
            {
                DateTime strDate = Convert.ToDateTime(this.Model.DataObject["FSTADATE"]);
                DateTime endDate = Convert.ToDateTime(this.Model.DataObject["FENDDATE"]);
                this.Model.SetValue("FSTADATE", strDate.AddDays(-1), 0);
                this.Model.SetValue("FENDDATE", endDate.AddDays(-1), 0);
                this.Model.DeleteEntryData("FKHWLZ");
                this.View.UpdateView("Kb61c946ccfd787f9");
                //DynamicObjectCollection allDatas = GetDatas(strDate, endDate);
                //if (allDatas.Count > 0)
                //{
                //    this.Model.BatchCreateNewEntryRow("FKHWLZ", allDatas.Count);
                //    //var rows = this.Model.DataObject["FKHWLZ"] as DynamicObjectCollection;
                //    for (int i = 0; i < allDatas.Count; i++)
                //    {
                //        this.Model.SetValue("FCPLB", allDatas[i]["FCPLB"], i);
                //        this.Model.SetValue("FXSTD", allDatas[i]["FXSTD"], i);
                //        this.Model.SetValue("FXSY", allDatas[i]["FXSY"], i);
                //        this.Model.SetValue("FNUMBER", allDatas[i]["FNUMBER"], i);
                //        this.Model.SetValue("FNAME", allDatas[i]["FNAME"], i);
                //        this.Model.SetValue("FQCYE", allDatas[i]["FQCYE"], i);
                //        this.Model.SetValue("FBQXL", allDatas[i]["FBQXL"], i);
                //        this.Model.SetValue("FBQQTXL", allDatas[i]["FBQQTXL"], i);
                //        this.Model.SetValue("FBQQTXE", allDatas[i]["FBQQTXE"], i);
                //        this.Model.SetValue("FBQXE", allDatas[i]["FBQXE"], i);
                //        this.Model.SetValue("FBQSKE", allDatas[i]["FBQSKE"], i);
                //        this.Model.SetValue("FBQYE", allDatas[i]["FBQYE"], i);
                //        this.Model.SetValue("FDQZQK", allDatas[i]["FDQZQK"], i);
                //    }
                //    this.View.UpdateView("FKHWLZ");
                //}
                //this.View.UpdateView();
            }
            //本期
            if (e.Key.Equals("FBQ"))
            {
                // 获取当前日期和时间
                DateTime now = DateTime.Now; // 或 DateTime.Today

                // 本月的第一天
                DateTime firstDayOfMonth = new DateTime(now.Year, now.Month, 1);

                // 本月的最后一天（通过计算下月第一天减1天）
                DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);

                this.Model.SetValue("FSTADATE", firstDayOfMonth, 0);
                this.Model.SetValue("FENDDATE", lastDayOfMonth, 0);
                this.Model.DeleteEntryData("FKHWLZ");
                this.View.UpdateView("Kb61c946ccfd787f9");
                //DynamicObjectCollection allDatas = GetDatas(strDate, endDate);
                //if (allDatas.Count > 0)
                //{
                //    this.Model.BatchCreateNewEntryRow("FKHWLZ", allDatas.Count);
                //    //var rows = this.Model.DataObject["FKHWLZ"] as DynamicObjectCollection;
                //    for (int i = 0; i < allDatas.Count; i++)
                //    {
                //        this.Model.SetValue("FCPLB", allDatas[i]["FCPLB"], i);
                //        this.Model.SetValue("FXSTD", allDatas[i]["FXSTD"], i);
                //        this.Model.SetValue("FXSY", allDatas[i]["FXSY"], i);
                //        this.Model.SetValue("FNUMBER", allDatas[i]["FNUMBER"], i);
                //        this.Model.SetValue("FNAME", allDatas[i]["FNAME"], i);
                //        this.Model.SetValue("FQCYE", allDatas[i]["FQCYE"], i);
                //        this.Model.SetValue("FBQXL", allDatas[i]["FBQXL"], i);
                //        this.Model.SetValue("FBQQTXL", allDatas[i]["FBQQTXL"], i);
                //        this.Model.SetValue("FBQQTXE", allDatas[i]["FBQQTXE"], i);
                //        this.Model.SetValue("FBQXE", allDatas[i]["FBQXE"], i);
                //        this.Model.SetValue("FBQSKE", allDatas[i]["FBQSKE"], i);
                //        this.Model.SetValue("FBQYE", allDatas[i]["FBQYE"], i);
                //        this.Model.SetValue("FDQZQK", allDatas[i]["FDQZQK"], i);
                //    }
                //    this.View.UpdateView("FKHWLZ");
                //}
                //this.View.UpdateView();
            }
            //上期
            if (e.Key.Equals("FSQ"))
            {
                // 获取当前日期
                DateTime today = DateTime.Today;

                // 获取上个月的第一天
                DateTime firstDayOfLastMonth = new DateTime(today.Year, today.Month, 1).AddMonths(-1);

                // 获取上个月的最后一天
                DateTime lastDayOfLastMonth = new DateTime(today.Year, today.Month, 1).AddDays(-1);

                this.Model.SetValue("FSTADATE", firstDayOfLastMonth, 0);
                this.Model.SetValue("FENDDATE", lastDayOfLastMonth, 0);
                this.Model.DeleteEntryData("FKHWLZ");
                this.View.UpdateView("Kb61c946ccfd787f9");
                //DynamicObjectCollection allDatas = GetDatas(strDate, endDate);
                //if (allDatas.Count > 0)
                //{
                //    this.Model.BatchCreateNewEntryRow("FKHWLZ", allDatas.Count);
                //    //var rows = this.Model.DataObject["FKHWLZ"] as DynamicObjectCollection;
                //    for (int i = 0; i < allDatas.Count; i++)
                //    {
                //        this.Model.SetValue("FCPLB", allDatas[i]["FCPLB"], i);
                //        this.Model.SetValue("FXSTD", allDatas[i]["FXSTD"], i);
                //        this.Model.SetValue("FXSY", allDatas[i]["FXSY"], i);
                //        this.Model.SetValue("FNUMBER", allDatas[i]["FNUMBER"], i);
                //        this.Model.SetValue("FNAME", allDatas[i]["FNAME"], i);
                //        this.Model.SetValue("FQCYE", allDatas[i]["FQCYE"], i);
                //        this.Model.SetValue("FBQXL", allDatas[i]["FBQXL"], i);
                //        this.Model.SetValue("FBQQTXL", allDatas[i]["FBQQTXL"], i);
                //        this.Model.SetValue("FBQQTXE", allDatas[i]["FBQQTXE"], i);
                //        this.Model.SetValue("FBQXE", allDatas[i]["FBQXE"], i);
                //        this.Model.SetValue("FBQSKE", allDatas[i]["FBQSKE"], i);
                //        this.Model.SetValue("FBQYE", allDatas[i]["FBQYE"], i);
                //        this.Model.SetValue("FDQZQK", allDatas[i]["FDQZQK"], i);
                //    }
                //    this.View.UpdateView("FKHWLZ");
                //}
                //this.View.UpdateView();
            }
        }

        private string GetBjf(string bjf)
        {
            string str = "==";
            if (bjf == "0")
            {
                str = ">=";
            }
            if (bjf == "1")
            {
                str = ">";
            }
            if (bjf == "2")
            {
                str = "==";
            }
            if (bjf == "3")
            {
                str = "<";
            }
            if (bjf == "4")
            {
                str = "<=";
            }
            return str;
        }

        public override void AfterBindData(EventArgs e)
        {
            if (BindData()) { return; }
            base.AfterBindData(e);
            this.View.GetControl<TabControl>("F_POSW_TAB_T7S").SetFireSelChanged(true);
            string strDate = DateTime.Now.ToString("yyyy-MM-dd");
            string endDate = DateTime.Now.ToString("yyyy-MM-dd");
            this.Model.SetValue("FSTADATE", DateTime.Now, 0);
            this.Model.SetValue("FENDDATE", DateTime.Now, 0);
            object res = this.Model.OpenParameter.GetCustomParameter("CustId");
            object orgId = this.Model.OpenParameter.GetCustomParameter("OrgId");
            var curOrgId = this.Context.CurrentOrganizationInfo.ID;
            if (orgId == null)
            {
                orgId = curOrgId;
            }
            this.Model.SetItemValueByID("F_POSW_Base_xvz", res, 0);
            this.Model.SetItemValueByID("FORG", orgId, 0);
            this.View.UpdateView("F_POSW_Base_xvz", 0);
            this.View.UpdateView("FORG", 0);
            var backColor1 = "#CCCFCC";
            var backColor2 = "#F9F8BE";
            var grid = this.View.GetControl<EntryGrid>("FKHWLZ");
            grid.Enabled = false;
            if (res == null)
            {
                this.Model.DeleteEntryData("FKHWLZ");
                DynamicObjectCollection allDatas = GetDatas(strDate, endDate, "");
                if (allDatas.Count > 0)
                {
                    this.Model.BatchCreateNewEntryRow("FKHWLZ", allDatas.Count);
                    var jo = new JSONObject();
                    for (int i = 0; i < allDatas.Count; i++)
                    {
                        this.Model.SetValue("FCPLB", allDatas[i]["FCPLB"], i);
                        this.Model.SetValue("FXSTD", allDatas[i]["FXSTD"], i);
                        this.Model.SetValue("FXSBM", allDatas[i]["FXSBM"], i);
                        this.Model.SetValue("FXSY", allDatas[i]["FXSY"], i);
                        this.Model.SetValue("FNUMBER", allDatas[i]["FNUMBER"], i);
                        this.Model.SetValue("FNAME", allDatas[i]["FNAME"], i);
                        this.Model.SetValue("FQCYE", allDatas[i]["FQCYE"], i);
                        this.Model.SetValue("FBQXL", allDatas[i]["FBQXL"], i);
                        this.Model.SetValue("FBQQTXL", allDatas[i]["FBQQTXL"], i);
                        this.Model.SetValue("FBQQTXE", allDatas[i]["FBQQTXE"], i);
                        this.Model.SetValue("FBQXE", allDatas[i]["FBQXE"], i);
                        this.Model.SetValue("FBQSKE", allDatas[i]["FBQSKE"], i);
                        this.Model.SetValue("FBQYE", allDatas[i]["FBQYE"], i);
                        this.Model.SetValue("FDQZQK", allDatas[i]["FDQZQK"], i);
                        this.Model.SetItemValueByID("FCUSTID", allDatas[i]["FCUSTID"], i);

                        //if (i % 2 == 1)
                        //{
                        //    grid.SetRowBackcolor(backColor1, i);
                        //    //Logger.Info("FKHWLZ", $"Setting row {i} background color:{backColor1}");
                        //}
                        //else
                        //{
                        //    grid.SetRowBackcolor(backColor2, i);
                        //    //Logger.Info("FKHWLZ", $"Setting row {i} background color:{backColor2}");
                        //}
                    }
                    this.View.UpdateView("FKHWLZ");
                }
            }
            else
            {
                bool isFree = Convert.ToBoolean(this.Model.DataObject["F_POSW_CheckBox_c6t"]);
                long custId = Convert.ToInt64(res);
                DynamicObjectCollection allDatas = GetDatas2(strDate, endDate, custId);
                //主表 1
                if (allDatas.Count > 0)
                {
                    this.Model.DeleteEntryData("FKHWLZ");
                    this.Model.BatchCreateNewEntryRow("FKHWLZ", allDatas.Count);
                    //var rows = this.Model.DataObject["FKHWLZ"] as DynamicObjectCollection;
                    for (int i = 0; i < allDatas.Count; i++)
                    {
                        this.Model.SetValue("FCPLB", allDatas[i]["FCPLB"], i);
                        this.Model.SetValue("FXSTD", allDatas[i]["FXSTD"], i);
                        this.Model.SetValue("FXSBM", allDatas[i]["FXSBM"], i);
                        this.Model.SetValue("FXSY", allDatas[i]["FXSY"], i);
                        this.Model.SetValue("FNUMBER", allDatas[i]["FNUMBER"], i);
                        this.Model.SetValue("FNAME", allDatas[i]["FNAME"], i);
                        this.Model.SetValue("FQCYE", allDatas[i]["FQCYE"], i);
                        this.Model.SetValue("FBQXL", allDatas[i]["FBQXL"], i);
                        this.Model.SetValue("FBQQTXL", allDatas[i]["FBQQTXL"], i);
                        this.Model.SetValue("FBQQTXE", allDatas[i]["FBQQTXE"], i);
                        this.Model.SetValue("FBQXE", allDatas[i]["FBQXE"], i);
                        this.Model.SetValue("FBQSKE", allDatas[i]["FBQSKE"], i);
                        this.Model.SetValue("FBQYE", allDatas[i]["FBQYE"], i);
                        this.Model.SetValue("FDQZQK", allDatas[i]["FDQZQK"], i);
                        this.Model.SetItemValueByID("FCUSTID", allDatas[i]["FCUSTID"], i);
                        //if (i % 2 == 1)
                        //{
                        //    grid.SetRowBackcolor(backColor1, i);
                        //}
                        //else
                        //{

                        //    grid.SetRowBackcolor(backColor2, i);
                        //}
                    }
                    this.View.UpdateView("FKHWLZ");
                }
                //对账单(副表) 2
                DynamicObjectCollection t2Datas = GetT2Datas(strDate, endDate, custId);
                if (t2Datas.Count > 0)
                {
                    this.Model.DeleteEntryData("F_POSW_Entity_mpw");
                    this.Model.BatchCreateNewEntryRow("F_POSW_Entity_mpw", t2Datas.Count);
                    for (int i = 0; i < t2Datas.Count; i++)
                    {
                        this.Model.SetValue("FSEQ", t2Datas[i]["FSEQ"], i);
                        this.Model.SetValue("F_POSW_Date_1bv", t2Datas[i]["FDATE"], i);
                        this.Model.SetValue("F_POSW_Text_0ne", t2Datas[i]["FBILLNO"], i);
                        this.Model.SetValue("F_POSW_Text_uo3", t2Datas[i]["FZY"], i);
                        this.Model.SetValue("F_POSW_Amount_rec", t2Datas[i]["FXSJE"], i);
                        this.Model.SetValue("F_POSW_Amount_bpq", t2Datas[i]["FHKJE"], i);
                        this.Model.SetValue("F_POSW_Amount_d7o", t2Datas[i]["FQMQK"], i);
                        this.Model.SetValue("FCPQty", t2Datas[i]["FCPQty"], i);
                        this.Model.SetValue("FDZQty", t2Datas[i]["FDZQty"], i);
                        this.Model.SetValue("FYearMonth", t2Datas[i]["FYearMonth"], i);
                    }
                    this.View.UpdateView("F_POSW_Entity_mpw");
                }
                //明细对账单（副表） 3
                DynamicObjectCollection t3Datas = GetT3Datas(strDate, endDate, custId);
                if (t3Datas.Count > 0)
                {
                    this.Model.DeleteEntryData("F_POSW_Entity_60c");
                    this.Model.BatchCreateNewEntryRow("F_POSW_Entity_60c", t3Datas.Count);
                    for (int i = 0; i < t3Datas.Count; i++)
                    {
                        this.Model.SetValue("FSEQ2", t3Datas[i]["FSEQ"], i);
                        this.Model.SetValue("F_POSW_Date_e9h", t3Datas[i]["FDATE"], i);
                        this.Model.SetValue("F_POSW_Text_zg7", t3Datas[i]["FBILLNO"], i);
                        this.Model.SetValue("F_POSW_Text_9l8", t3Datas[i]["FZY"], i);
                        this.Model.SetValue("F_POSW_Base_7w7", t3Datas[i]["FDW"], i);
                        this.Model.SetValue("F_POSW_Qty_xxu", t3Datas[i]["FSL"], i);
                        this.Model.SetValue("F_POSW_Decimal_ikt", t3Datas[i]["FZL"], i);
                        this.Model.SetValue("F_POSW_Price_xfa", t3Datas[i]["FKPJ"], i);
                        this.Model.SetValue("F_POSW_Decimal_f5d", t3Datas[i]["FZK"], i);
                        this.Model.SetValue("F_POSW_Price_vzn", t3Datas[i]["FXSJ"], i);
                        this.Model.SetValue("F_POSW_Amount_y10", t3Datas[i]["FXSJE"], i);
                        this.Model.SetValue("F_POSW_Amount_ito", t3Datas[i]["FHKJE"], i);
                        this.Model.SetValue("F_POSW_Amount_vxl", t3Datas[i]["FQMQK"], i);
                    }
                    this.View.UpdateView("F_POSW_Entity_60c");
                }
                //客户品种销售（副表） 4
                DynamicObjectCollection t4Datas = GetT4Datas(strDate, endDate, custId, isFree);
                if (t4Datas.Count > 0)
                {
                    this.Model.DeleteEntryData("F_POSW_Entity_vbc");
                    this.Model.BatchCreateNewEntryRow("F_POSW_Entity_vbc", t4Datas.Count);
                    for (int i = 0; i < t4Datas.Count; i++)
                    {
                        this.Model.SetItemValueByNumber("F_POSW_Base_aec", t4Datas[i]["FPZBH"].ToString(), i);
                        this.Model.SetValue("F_POSW_Text_n6p", t4Datas[i]["FWLMC"], i);
                        this.Model.SetValue("F_POSW_Decimal_i6k", t4Datas[i]["FKG"], i);
                        this.Model.SetValue("F_POSW_Decimal_amv", t4Datas[i]["FBAO"], i);
                        this.Model.SetValue("F_POSW_Decimal_7wd", t4Datas[i]["FTUN"], i);
                        this.Model.SetValue("F_POSW_Amount_gmk", t4Datas[i]["FSJJE"], i);
                    }
                    this.View.UpdateView("F_POSW_Entity_vbc");
                }
                //销售分类（副表） 5
                DynamicObjectCollection t5Datas = GetT5Datas(strDate, endDate, custId, isFree);
                if (t5Datas.Count > 0)
                {
                    this.Model.DeleteEntryData("F_POSW_Entity_6yb");
                    this.Model.BatchCreateNewEntryRow("F_POSW_Entity_6yb", t5Datas.Count);
                    for (int i = 0; i < t5Datas.Count; i++)
                    {
                        this.Model.SetValue("F_POSW_Text_ii9", t5Datas[i]["FXSFZ"], i);
                        this.Model.SetValue("F_POSW_Decimal_rks", t5Datas[i]["FKG"], i);
                        this.Model.SetValue("F_POSW_Decimal_71r", t5Datas[i]["FBAO"], i);
                        this.Model.SetValue("F_POSW_Decimal_f02", t5Datas[i]["FTUN"], i);
                        this.Model.SetValue("F_POSW_Amount_15v", t5Datas[i]["FSJJE"], i);
                    }
                    this.View.UpdateView("F_POSW_Entity_6yb");
                }
            }

            //SetGridColor(this.View);
        }

        private void SetGridColor(IDynamicFormView view)
        {
            var backColor1 = "#808080";
            var backColor2 = "#FFFFFF";
            var grid = view.GetControl<EntryGrid>("FKHWLZ");
            var rowCount = this.View.Model.GetEntryRowCount("FKHWLZ");
            if (rowCount % 2 == 1)
            {
                grid.SetBackcolor("FKHWLZ", backColor1, rowCount);
            }
            else
            {
                grid.SetBackcolor("FKHWLZ", backColor2, rowCount);
            }
        }

        private bool BindData()
        {
           
            return false;
        }

        private DynamicObjectCollection GetT5Datas(string strDate, string endDate, long custId, bool isFree)
        {
            string sql = "";
            if (isFree)
            {
                sql = string.Format($@"/*dialect*/ 
                SELECT 
                    T4.FNAME FXSFZ,
	                SUM(T2.FSALBASEQTY) FKG,
	                SUM(T1.FPRICEQTY) FBAO,
	                SUM(T2.FSALQTY) FTUN,
	                SUM(T1.FALLAMOUNTFOR) FSJJE
                FROM t_AR_receivable T0
                JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
                JOIN T_AR_RECEIVABLEENTRY_O T2 ON T2.FENTRYID=T1.FENTRYID
                JOIN T_BD_MATERIAL T3 ON T3.FMATERIALID=T1.FMATERIALID
                JOIN T_BD_MATERIALGROUP_L T4 ON T4.FID=t3.FMATERIALGROUP AND T4.FLOCALEID=2052
                WHERE T0.FDATE >= '{strDate}' AND T0.FDATE<='{endDate}'  AND T0.FCUSTOMERID = {custId}
                GROUP BY T4.FNAME ");
            }
            else
            {
                sql = string.Format($@"/*dialect*/ 
                SELECT 
                    T4.FNAME FXSFZ,
	                SUM(T2.FSALBASEQTY) FKG,
	                SUM(T1.FPRICEQTY) FBAO,
	                SUM(T2.FSALQTY) FTUN,
	                SUM(T1.FALLAMOUNTFOR) FSJJE
                FROM t_AR_receivable T0
                JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
                JOIN T_AR_RECEIVABLEENTRY_O T2 ON T2.FENTRYID=T1.FENTRYID
                JOIN T_BD_MATERIAL T3 ON T3.FMATERIALID=T1.FMATERIALID
                JOIN T_BD_MATERIALGROUP_L T4 ON T4.FID=t3.FMATERIALGROUP AND T4.FLOCALEID=2052
                WHERE T0.FDATE >= '{strDate}' AND T0.FDATE<='{endDate}'  AND T0.FCUSTOMERID = {custId} AND T2.FISFREE=0
                GROUP BY T4.FNAME ");
            }

            DynamicObjectCollection doc = DBUtils.ExecuteDynamicObject(this.Context, sql);
            return doc;
        }

        private DynamicObjectCollection GetT4Datas(string strDate, string endDate, long custId, bool isFree)
        {
            string sql = "";
            if (isFree)
            {
                sql = string.Format($@"/*dialect*/ 
                    SELECT 
                        T3.FNUMBER FPZBH,
	                    T4.FNAME FWLMC,
	                    SUM(T2.FSALBASEQTY) FKG,
	                    SUM(T1.FPRICEQTY) FBAO,
	                    SUM(T2.FSALQTY) FTUN,
	                    SUM(T1.FALLAMOUNTFOR) FSJJE
                    FROM t_AR_receivable T0
                    JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
                    JOIN T_AR_RECEIVABLEENTRY_O T2 ON T2.FENTRYID=T1.FENTRYID
                    JOIN T_BD_MATERIAL T3 ON T3.FMATERIALID=T1.FMATERIALID
                    JOIN T_BD_MATERIAL_L T4 ON T4.FMATERIALID=T3.FMATERIALID AND T4.FLOCALEID=2052
                    WHERE T0.FDATE >= '{strDate}' AND T0.FDATE<='{endDate}'  AND T0.FCUSTOMERID = {custId}
                    GROUP BY T3.FNUMBER,T4.FNAME ");
            }
            else
            {
                sql = string.Format($@"/*dialect*/ 
                    SELECT 
                        T3.FNUMBER FPZBH,
	                    T4.FNAME FWLMC,
	                    SUM(T2.FSALBASEQTY) FKG,
	                    SUM(T1.FPRICEQTY) FBAO,
	                    SUM(T2.FSALQTY) FTUN,
	                    SUM(T1.FALLAMOUNTFOR) FSJJE
                    FROM t_AR_receivable T0
                    JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
                    JOIN T_AR_RECEIVABLEENTRY_O T2 ON T2.FENTRYID=T1.FENTRYID
                    JOIN T_BD_MATERIAL T3 ON T3.FMATERIALID=T1.FMATERIALID
                    JOIN T_BD_MATERIAL_L T4 ON T4.FMATERIALID=T3.FMATERIALID AND T4.FLOCALEID=2052
                    WHERE T0.FDATE >= '{strDate}' AND T0.FDATE<='{endDate}'  AND T0.FCUSTOMERID = {custId} AND T2.FISFREE=0
                    GROUP BY T3.FNUMBER,T4.FNAME ");
            }

            DynamicObjectCollection doc = DBUtils.ExecuteDynamicObject(this.Context, sql);
            return doc;
        }

        private DynamicObjectCollection GetT3Datas(string strDate, string endDate, long custId)
        {
            string sql = string.Format($@"/*dialect*/ 
            -- 期初余额
            SELECT 
	            A1.FDATE,A1.FBILLNO,A1.FZY,A1.FDW,A1.FSL,A1.FZL,A1.FKPJ,A1.FZK,A1.FXSJ,A1.FXSJE,A1.FHKJE,
	            ISNULL(A1.FQMQK,0)-ISNULL(A2.FQCSK,0) FQMQK,A1.FQJ,A1.SortOrder
            INTO #TQJ
            FROM (
            SELECT 
	            TAA.FCUSTID,
                '' FDATE,
                '' FBILLNO,
                '期初余额' FZY,
                '' FDW,
                0 FSL,
                0 FZL,
                0 FKPJ,
                0 FZK,
                0 FXSJ,
                0 FXSJE,
                0 FHKJE,
                SUM(TAA.FQMQK) FQMQK,
                '期初' FQJ,
                0 AS SortOrder
            FROM (
            SELECT 
                T0.FCUSTOMERID FCUSTID,
	            T0.FDATE FDATE,
                '' FBILLNO,
                '期初余额' FZY,
                '' FDW,
                0 FSL,
                0 FZL,
                0 FKPJ,
                0 FZK,
                0 FXSJ,
                0 FXSJE,
                0 FHKJE,
                T1.FALLAMOUNTFOR FQMQK,
                '期初' FQJ,
                0 AS SortOrder
            FROM t_AR_receivable T0
            JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID 
            WHERE T0.FDATE < '{strDate}' AND T0.FCUSTOMERID = {custId} AND T0.FSETACCOUNTTYPE=3
            UNION ALL
	            SELECT 
		            T0.FCONTACTUNIT,
		            T0.FDATE,
		            '' FBILLNO,
		            '期初余额' AS FZY,
		            '' FDW,
		            0 FSL,
		            0 FZL,
		            0 FKPJ,
		            0 FZK,
		            0 FXSJ,
		            0 FXSJE,
		            0 FHKJE,
		            T1.FAMOUNT FQMQK,
		            '期初' FQJ,
		            0 AS SortOrder
	            FROM T_AR_OtherRecAble T0
	            JOIN T_AR_OtherRecAbleENTRY T1 ON T1.FID=T0.FID
	            WHERE T0.FCONTACTUNITTYPE ='BD_Customer' AND T0.FDATE < '{strDate}'
		            AND T0.FCONTACTUNIT={custId}
	            ) TAA
            GROUP BY TAA.FCUSTID
            ) A1
            FULL JOIN (
            SELECT 
            T0.FCONTACTUNIT AS FCUSTID,
            SUM(T1.FREALRECAMOUNT) AS FQCSK
            FROM T_AR_RECEIVEBILL T0
            JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
            WHERE T0.FCONTACTUNITTYPE = 'BD_Customer' AND T0.FCONTACTUNIT={custId}
            AND T0.FDATE < '{strDate}'
            GROUP BY T0.FCONTACTUNIT
            ) A2 ON A2.FCUSTID = A1.FCUSTID

            -- 本期应收
            SELECT 
                T0.FDATE FDATE,
                T0.FBILLNO,
                ISNULL(T4.FNAME,'')+ T1.FCOMMENT FZY,
                T3.FNAME FDW,
                T1.FPRICEQTY FSL,
                T2.FSALQTY FZL,
                T1.FTAXPRICE FKPJ,
                T1.F_QNXN_PRICE_DJZK FZK,
                T1.FPRICEWITHTAX FXSJ,
                T1.FALLAMOUNTFOR FXSJE,
                0 FHKJE,
                0 FQMQK,
                '本期' FQJ,
                1 AS SortOrder
            INTO #TBQYS
            FROM t_AR_receivable T0
            JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
            JOIN T_AR_RECEIVABLEENTRY_O T2 ON T2.FENTRYID = T1.FENTRYID
            JOIN T_BD_UNIT_L T3 ON T3.FUNITID = T1.FPRICEUNITID AND T3.FLOCALEID = 2052
            LEFT JOIN T_BD_MATERIAL_L T4 ON T4.FMATERIALID = T1.FMATERIALID AND T4.FLOCALEID = 2052
            WHERE T0.FDATE >= '{strDate}' AND T0.FDATE <= '{endDate}' AND T0.FCUSTOMERID = {custId} AND T0.FSETACCOUNTTYPE=3

            --
            SELECT 
	            T0.FDATE FDATE,
                T0.FBILLNO,
                T0.FREMARK FZY,
                '' FDW,
                0 FSL,
                0 FZL,
                0 FKPJ,
                0 FZK,
                0 FXSJ,
                T1.FAMOUNT FXSJE,
                0 FHKJE,
                0 FQMQK,
                '本期' FQJ,
                1 AS SortOrder
            INTO #TQTYS
            FROM T_AR_OtherRecAble T0
            JOIN T_AR_OtherRecAbleENTRY T1 ON T1.FID=T0.FID
            WHERE T0.FCONTACTUNITTYPE ='BD_Customer' AND T0.FDATE >= '{strDate}' AND T0.FDATE <= '{endDate}'
            AND T0.FCONTACTUNIT={custId}

            -- 本期收款
            SELECT 
                T0.FDATE,
                T0.FBILLNO,
                T0.FREMARK FZY,
                '' FDW,
                T1.FQTY FSL,
                0 FZL,
                0 FKPJ,
                0 FZK,
                0 FXSJ,
                0 FXSJE,
                T1.FREALRECAMOUNTFOR FHKJE,
                0 FQMQK,
                '本期' FQJ,
                2 AS SortOrder
            INTO #TBQSK
            FROM T_AR_RECEIVEBILL T0
            JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
            WHERE T0.FDATE >= '{strDate}' AND T0.FDATE <= '{endDate}'
                AND T0.FCONTACTUNITTYPE = 'BD_Customer' AND FCONTACTUNIT = {custId}

            -- 合并所有数据并按日期和类型排序
            SELECT 
                ROW_NUMBER() OVER (ORDER BY FDATE, SortOrder, FBILLNO) AS FSEQ,
                FDATE, 
                FBILLNO, 
                FZY, 
                FDW, 
                FSL, 
                FZL, 
                FKPJ,
                FZK,
                FXSJ,
                FXSJE,
                FHKJE,
                FQMQK,
                FQJ
            INTO #AllData
            FROM (
                SELECT * FROM #TQJ
                UNION ALL
                SELECT * FROM #TBQYS
                UNION ALL
	            SELECT * FROM #TQTYS
                UNION ALL
                SELECT * FROM #TBQSK
            ) H;

            -- 使用窗口函数计算累计余额（替代递归CTE）
            SELECT 
                FSEQ,
                FDATE, 
                FBILLNO, 
                FZY, 
                FDW, 
                FSL, 
                FZL, 
                FKPJ,
                FZK,
                FXSJ,
                FXSJE,
                FHKJE,
                -- 核心优化：使用窗口函数计算运行余额
                FIRST_VALUE(FQMQK) OVER (ORDER BY FSEQ) + 
                SUM(FXSJE - FHKJE) OVER (ORDER BY FSEQ ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS FQMQK,
                FQJ
            FROM #AllData
            ORDER BY FSEQ;

            -- 清理临时表
            DROP TABLE #TQJ, #TBQYS, #TBQSK, #AllData; ");
            DynamicObjectCollection doc = DBUtils.ExecuteDynamicObject(this.Context, sql);
            return doc;
        }

        private DynamicObjectCollection GetT2Datas(string strDate, string endDate, long custId)
        {
            string sql = string.Format($@"/*dialect*/ 
            -- 期初余额
            SELECT 
                COALESCE(A1.FCUSTID, A2.FCUSTID) AS FCUSTID,
                '' FDATE,
                A1.FYearMonth,
                A1.FBILLNO,
                A1.FZY,
                A1.FXSJE,
                A1.FHKJE,
                ISNULL(A1.FQMQK, 0) - ISNULL(A2.FQCSK, 0) AS FQMQK,
                A1.FQJ,
                A1.FCPQty,
	            A1.FDZQty,
                A1.SortOrder
            INTO #TQJ
            FROM (
                SELECT MAX(TAA.FDATE) FDATE,MAX(TAA.FYearMonth) FYearMonth,'' FBILLNO,'期初余额' FZY,0 FXSJE,0 FHKJE,SUM(TAA.FQMQK) FQMQK,'期初' FQJ,TAA.FCUSTID,
                SUM(TAA.FCPQty) FCPQty,SUM(TAA.FDZQty) FDZQty,0 SortOrder
	            FROM (
                SELECT 
                    T0.FDATE AS FDATE,
                    CONCAT(DATEPART(YEAR, FDATE), '年',RIGHT('0' + CAST(DATEPART(MONTH, T0.FDATE) AS VARCHAR(2)), 2), '月') AS FYearMonth,
                    '' AS FBILLNO,
                    '期初余额' AS FZY,
                    0 AS FXSJE,
                    0 AS FHKJE,
                    T1.FALLAMOUNTFOR AS FQMQK,
                    '期初' AS FQJ,
                    T0.FCUSTOMERID AS FCUSTID, 
                    CASE WHEN T4.FNAME='商品料' THEN T2.FSALQTY ELSE 0 END FCPQty,
		            CASE WHEN T4.FNAME='定制料' THEN T2.FSALQTY ELSE 0 END FDZQty,
                    0 AS SortOrder
                FROM t_AR_receivable T0
                JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
                JOIN T_AR_RECEIVABLEENTRY_O T2 ON T2.FENTRYID=T1.FENTRYID
	            LEFT JOIN t_BD_MaterialSale T3 ON T3.FMATERIALID=T1.FMATERIALID
	            LEFT JOIN T_BD_MATERIALSALGROUP_L T4 ON T4.FID=T3.FSALGROUP AND T4.FLOCALEID=2052
                WHERE T0.FDATE < '{strDate}' 
                  AND T0.FCUSTOMERID = {custId} AND T0.FSETACCOUNTTYPE=3
                UNION ALL
	            SELECT 
		            T0.FDATE,
                    CONCAT(DATEPART(YEAR, FDATE), '年',RIGHT('0' + CAST(DATEPART(MONTH, T0.FDATE) AS VARCHAR(2)), 2), '月') AS FYearMonth,
		            '' FBILLNO,
		            '期初余额' AS FZY,
		            0 AS FXSJE,
                    0 AS FHKJE,
		            T1.FAMOUNT FQMQK,
		            '期初' AS FQJ,
		            T0.FCONTACTUNIT FCUSTID,
                    0 FCPQty,
		            0 FDZQty,
		            0 AS SortOrder
	            FROM T_AR_OtherRecAble T0
	            JOIN T_AR_OtherRecAbleENTRY T1 ON T1.FID=T0.FID
	            WHERE T0.FCONTACTUNITTYPE ='BD_Customer' AND T0.FDATE < '{strDate}'
		            AND T0.FCONTACTUNIT={custId}
                ) TAA
	            GROUP BY TAA.FCUSTID
            ) A1
            FULL JOIN (  
                SELECT 
                    T0.FCONTACTUNIT AS FCUSTID,
                    SUM(T1.FREALRECAMOUNT) AS FQCSK
                FROM T_AR_RECEIVEBILL T0
                JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
                WHERE T0.FCONTACTUNITTYPE = 'BD_Customer' 
                  AND T0.FCONTACTUNIT = {custId}
                  AND T0.FDATE < '{strDate}'
                GROUP BY T0.FCONTACTUNIT
            ) A2 ON A2.FCUSTID = A1.FCUSTID;

            -- 本期应收
            SELECT 
                MAX(T0.FCUSTOMERID) FCUSTOMERID,
	            MAX(T0.FDATE) FDATE,
                MAX(CONCAT(DATEPART(YEAR, FDATE), '年',RIGHT('0' + CAST(DATEPART(MONTH, T0.FDATE) AS VARCHAR(2)), 2), '月')) AS FYearMonth,
                T0.FBILLNO,
                MAX(T0.FREMARK) FZY,
                SUM(T1.FALLAMOUNTFOR) FXSJE,
                0 FHKJE,
                0 FQMQK,
                '本期' FQJ,
                SUM(CASE WHEN T4.FNAME='商品料' THEN T2.FSALQTY ELSE 0 END) FCPQty,
	            SUM(CASE WHEN T4.FNAME='定制料' THEN T2.FSALQTY ELSE 0 END) FDZQty,
                1 AS SortOrder
            INTO #TBQYS
            FROM t_AR_receivable T0
            JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
            JOIN T_AR_RECEIVABLEENTRY_O T2 ON T2.FENTRYID=T1.FENTRYID
            LEFT JOIN t_BD_MaterialSale T3 ON T3.FMATERIALID=T1.FMATERIALID
            LEFT JOIN T_BD_MATERIALSALGROUP_L T4 ON T4.FID=T3.FSALGROUP AND T4.FLOCALEID=2052
            WHERE T0.FCUSTOMERID = {custId} AND T0.FSETACCOUNTTYPE=3
                AND T0.FDATE >= '{strDate}' AND T0.FDATE <= '{endDate}'
            GROUP BY T0.FBILLNO

            --
            SELECT 
                MAX(T0.FCONTACTUNIT) FCONTACTUNIT,
	            MAX(T0.FDATE) FDATE,
                MAX(CONCAT(DATEPART(YEAR, FDATE), '年',RIGHT('0' + CAST(DATEPART(MONTH, T0.FDATE) AS VARCHAR(2)), 2), '月')) AS FYearMonth,
                T0.FBILLNO,
                MAX(T0.FREMARK) FZY,
                SUM(T1.FAMOUNT) FXSJE,
                0 FHKJE,
                0 FQMQK,
                '本期' FQJ,
                0 FCPQty,
	            0 FDZQty,
                1 AS SortOrder
            INTO #TQTYS
            FROM T_AR_OtherRecAble T0
            JOIN T_AR_OtherRecAbleENTRY T1 ON T1.FID=T0.FID
            WHERE T0.FCONTACTUNITTYPE ='BD_Customer' AND T0.FDATE >= '{strDate}' AND T0.FDATE <= '{endDate}'
            AND T0.FCONTACTUNIT={custId}
            GROUP BY T0.FBILLNO

            -- 本期收款
            SELECT 
                MAX(T0.FCONTACTUNIT) FCONTACTUNIT,
	            MAX(T0.FDATE) FDATE,
                MAX(CONCAT(DATEPART(YEAR, FDATE), '年',RIGHT('0' + CAST(DATEPART(MONTH, T0.FDATE) AS VARCHAR(2)), 2), '月')) AS FYearMonth,
                T0.FBILLNO,
                MAX(T0.FREMARK) FZY,
                0 FXSJE,
                SUM(T1.FREALRECAMOUNTFOR) FHKJE,
                0 FQMQK,
                '本期' FQJ,
                0 FCPQty,
	            0 FDZQty,
                2 AS SortOrder
            INTO #TBQSK
            FROM T_AR_RECEIVEBILL T0
            JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
            WHERE T0.FDATE >= '{strDate}' AND T0.FDATE <= '{endDate}'
                AND T0.FCONTACTUNITTYPE = 'BD_Customer' AND FCONTACTUNIT = {custId}
            GROUP BY T0.FBILLNO

            -- 合并所有数据并按日期和类型排序
            SELECT 
                ROW_NUMBER() OVER (ORDER BY FDATE, SortOrder, FBILLNO) AS FSEQ,
                FDATE, 
                FYearMonth,
                FBILLNO, 
                FZY, 
                FXSJE, 
                FHKJE, 
                FQMQK, 
                FCPQty,
	            FDZQty,
                FQJ
            INTO #AllData
            FROM (
                SELECT * FROM #TQJ
                UNION ALL
                SELECT * FROM #TBQYS
                UNION ALL
                SELECT * FROM #TQTYS
                UNION ALL
                SELECT * FROM #TBQSK
            ) H;

            -- 使用窗口函数计算累计余额
            SELECT 
                FSEQ,
                FDATE,
                FYearMonth,
                FBILLNO,
                FZY,
                FXSJE,
                FHKJE,
                FCPQty,
	            FDZQty,
                -- 核心优化：使用SUM OVER计算运行余额
                FIRST_VALUE(FQMQK) OVER (ORDER BY FSEQ) 
                + SUM(FXSJE - FHKJE) OVER (ORDER BY FSEQ ROWS UNBOUNDED PRECEDING) AS FQMQK,
                FQJ
            FROM #AllData
            ORDER BY FSEQ;

            -- 清理临时表
            DROP TABLE #TQJ, #TBQYS, #TBQSK, #AllData,#TQTYS; ");
            DynamicObjectCollection doc = DBUtils.ExecuteDynamicObject(this.Context, sql);
            return doc;
        }

        private DynamicObjectCollection GetDatas2(string strDate, string endDate, long custId)
        {
            string sql = string.Format($@"/*dialect*/ 
    -- 创建基础客户信息CTE
    WITH BaseCustomer AS (
    SELECT 
        T0.FCUSTID,
        T1.FNAME AS FCPLB,
        T2.FNAME AS FXSTD,
        T5.FNAME AS FXSBM,
        T3.FNAME AS FXSY,
        T0.FNUMBER,
        T4.FNAME,
		-- 添加分组标识和行号
        ROW_NUMBER() OVER (
            PARTITION BY T1.FNAME--, T2.FNAME, T3.FNAME 
            ORDER BY T0.FNUMBER, T4.FNAME
        ) AS GroupRowNum1,
		-- 添加分组标识和行号
        ROW_NUMBER() OVER (
            PARTITION BY T1.FNAME,T2.FNAME
            ORDER BY T0.FNUMBER, T4.FNAME
        ) AS GroupRowNum2,
		-- 添加分组标识和行号
        ROW_NUMBER() OVER (
            PARTITION BY T1.FNAME,T2.FNAME, T3.FNAME 
            ORDER BY T0.FNUMBER, T4.FNAME
        ) AS GroupRowNum3,
		-- 添加排序列作为计算列
        ROW_NUMBER() OVER (
            ORDER BY T5.FNAME, T2.FNAME, T3.FNAME, T0.FNUMBER
        ) AS SortOrder
    FROM T_BD_CUSTOMER T0
    JOIN T_BD_CUSTOMERGROUP_L T1 
        ON T1.FID = T0.FPRIMARYGROUP 
        AND T1.FLOCALEID = 2052
    JOIN V_BD_OPERATORGROUP_L T2 
        ON T2.FENTRYID = T0.FSALGROUPID 
        AND T2.FLOCALEID = 2052
    JOIN V_BD_SALESMAN_L T3 
        ON T3.fid = T0.FSELLER 
        AND T3.FLOCALEID = 2052
    JOIN T_BD_CUSTOMER_L T4 
        ON T4.FCUSTID = T0.FCUSTID 
        AND T4.FLOCALEID = 2052
    JOIN T_BD_DEPARTMENT_L T5 ON T5.FDEPTID=T0.FSALDEPTID AND T5.FLOCALEID=2052
	    WHERE T0.FCUSTID={custId}
    ),
    ReceivableData AS (
        SELECT 
		        A.FCUSTID,SUM(A.FBQXL) FBQXL,SUM(A.FBQQTXL) FBQQTXL,SUM(A.FBQQTXE) FBQQTXE,SUM(A.FBQXE) FBQXE
	        FROM (    
        SELECT
        T0.FCUSTOMERID AS FCUSTID,
        SUM(CASE WHEN T3.FNAME = '商品料' THEN T11.FSALQTY ELSE 0 END) AS FBQXL,
        SUM(CASE WHEN T3.FNAME = '定制料' THEN T11.FSALQTY ELSE 0 END) AS FBQQTXL,
        SUM(CASE WHEN T3.FNAME = '定制料' THEN T1.FALLAMOUNT ELSE 0 END) AS FBQQTXE,
        SUM(T1.FALLAMOUNT) AS FBQXE
    FROM t_AR_receivable T0
    JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
    JOIN T_AR_RECEIVABLEENTRY_O T11 ON T11.FENTRYID = T1.FENTRYID
    JOIN t_BD_MaterialSale T2 ON T2.FMATERIALID=T1.FMATERIALID
	JOIN T_BD_MATERIALSALGROUP_L T3 ON T3.FID=T2.FSALGROUP AND T3.FLOCALEID=2052
    WHERE T0.FDATE BETWEEN '{strDate}' AND '{endDate}' AND T11.FISFREE=0
    GROUP BY T0.FCUSTOMERID
    UNION ALL(
		        SELECT 
			        T0.FCONTACTUNIT,
			        0 FBQXL,
			        0 FBQQTXL,
			        0 FBQQTXE,
			        SUM(T1.FAMOUNT) FBQXE
		        FROM T_AR_OtherRecAble T0
		        JOIN T_AR_OtherRecAbleENTRY T1 ON T1.FID=T0.FID
		        WHERE T0.FCONTACTUNITTYPE ='BD_Customer' AND T0.FDATE >= '{strDate}' AND T0.FDATE <= '{endDate}'
		        --AND T0.FCONTACTUNIT=119618
		        GROUP BY T0.FCONTACTUNIT
	        )) A
	        GROUP BY A.FCUSTID
    ),
    ReceiveBillData AS (
        SELECT
            T0.FCONTACTUNIT AS FCUSTID,
            SUM(T1.FREALRECAMOUNT) AS FREALRECAMOUNT
        FROM T_AR_RECEIVEBILL T0
        JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
        WHERE T0.FCONTACTUNITTYPE = 'BD_Customer'
            AND T0.FDATE BETWEEN '{strDate}' AND '{endDate}'
        GROUP BY T0.FCONTACTUNIT
    ),
    OpeningBalance AS (
        SELECT
            COALESCE(A1.FCUSTID, A2.FCUSTID) AS FCUSTID,
            ISNULL(A1.FQCYS, 0) - ISNULL(A2.FQCSK, 0) AS FQCYE
        FROM (
            SELECT TAA.FCUSTID,SUM(TAA.FQCYS ) FQCYS
		    FROM (
            SELECT 
                T0.FCUSTOMERID AS FCUSTID,
                T1.FALLAMOUNT AS FQCYS
            FROM t_AR_receivable T0
            JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
            WHERE T0.FDATE < '{strDate}' AND T0.FSETACCOUNTTYPE=3
            UNION ALL
		    SELECT 
			            T0.FCONTACTUNIT,
			            T1.FAMOUNT FQCYS
		            FROM T_AR_OtherRecAble T0
		            JOIN T_AR_OtherRecAbleENTRY T1 ON T1.FID=T0.FID
		            WHERE T0.FCONTACTUNITTYPE ='BD_Customer' AND T0.FDATE < '{strDate}'
		    ) TAA
            GROUP BY TAA.FCUSTID
        ) A1
        FULL JOIN (
            SELECT 
                T0.FCONTACTUNIT AS FCUSTID,
                SUM(T1.FREALRECAMOUNT) AS FQCSK
            FROM T_AR_RECEIVEBILL T0
            JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
            WHERE T0.FCONTACTUNITTYPE = 'BD_Customer'
                AND T0.FDATE < '{strDate}'
            GROUP BY T0.FCONTACTUNIT
        ) A2 ON A2.FCUSTID = A1.FCUSTID
    ),
    AllSK AS(
        SELECT
            COALESCE(A1.FCUSTID, A2.FCUSTID) AS FCUSTID,
            ISNULL(A1.FQCYS, 0) - ISNULL(A2.FQCSK, 0) AS QBSK
        FROM (
            SELECT TAA.FCUSTID,SUM(TAA.FQCYS ) FQCYS
		    FROM (
            SELECT 
                T0.FCUSTOMERID AS FCUSTID,
                T1.FALLAMOUNT AS FQCYS
            FROM t_AR_receivable T0
            JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
            WHERE T0.FSETACCOUNTTYPE=3
            UNION ALL
		    SELECT 
			            T0.FCONTACTUNIT,
			            T1.FAMOUNT FQCYS
		            FROM T_AR_OtherRecAble T0
		            JOIN T_AR_OtherRecAbleENTRY T1 ON T1.FID=T0.FID
		            WHERE T0.FCONTACTUNITTYPE ='BD_Customer' 
		    ) TAA
            GROUP BY TAA.FCUSTID
        ) A1
        FULL JOIN (
            SELECT 
                T0.FCONTACTUNIT AS FCUSTID,
                SUM(T1.FREALRECAMOUNT) AS FQCSK
            FROM T_AR_RECEIVEBILL T0
            JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
            WHERE T0.FCONTACTUNITTYPE = 'BD_Customer'
                --AND T0.FDATE < @STRDATE
            GROUP BY T0.FCONTACTUNIT
        ) A2 ON A2.FCUSTID = A1.FCUSTID
    )
    SELECT
        C.FCUSTID,
        C.FCPLB,
        C.FXSTD,
        C.FXSBM,
        C.FXSY,
	    -- 仅在第一行显示分组信息
        --CASE WHEN GroupRowNum1 = 1 THEN C.FCPLB ELSE NULL END AS FCPLB,
        --CASE WHEN GroupRowNum2 = 1 THEN C.FXSTD ELSE NULL END AS FXSTD,
        --CASE WHEN GroupRowNum3 = 1 THEN C.FXSY ELSE NULL END AS FXSY,
        C.FNUMBER,
        C.FNAME,
        ISNULL(O.FQCYE,0) FQCYE,
        ISNULL(R.FBQXL, 0) AS FBQXL,
        ISNULL(R.FBQQTXL, 0) AS FBQQTXL,
        ISNULL(R.FBQQTXE, 0) AS FBQQTXE,
        ISNULL(R.FBQXE, 0) AS FBQXE,
        ISNULL(RB.FREALRECAMOUNT, 0) AS FBQSKE,
	    ISNULL(O.FQCYE,0)+ISNULL(R.FBQXE, 0)-ISNULL(RB.FREALRECAMOUNT, 0) FBQYE,
	    ISNULL(SYSK.QBSK,0) FDQZQK,C.SortOrder
    INTO #TT1
    FROM BaseCustomer C
    LEFT JOIN ReceivableData R ON R.FCUSTID = C.FCUSTID
    LEFT JOIN ReceiveBillData RB ON RB.FCUSTID = C.FCUSTID
    LEFT JOIN OpeningBalance O ON O.FCUSTID = C.FCUSTID
    LEFT JOIN AllSK SYSK ON SYSK.FCUSTID=C.FCUSTID
    SELECT * FROM #TT1 ORDER BY SortOrder
    DROP TABLE #TT1 ");
            DynamicObjectCollection doc = DBUtils.ExecuteDynamicObject(this.Context, sql);
            return doc;
        }

        private DynamicObjectCollection GetDatas(string strDate, string endDate, string depStr)
        {
            string sql = string.Format($@"/*dialect*/ 
            -- 创建基础客户信息CTE
            WITH BaseCustomer AS (
            SELECT 
                T0.FCUSTID,
                T1.FNAME AS FCPLB,
                T2.FNAME AS FXSTD,
                T5.FNAME AS FXSBM,
                T3.FNAME AS FXSY,
                T0.FNUMBER,
                T4.FNAME,
		        -- 添加分组标识和行号
                ROW_NUMBER() OVER (
                    PARTITION BY T1.FNAME--, T2.FNAME, T3.FNAME 
                    ORDER BY T0.FNUMBER, T4.FNAME
                ) AS GroupRowNum1,
		        -- 添加分组标识和行号
                ROW_NUMBER() OVER (
                    PARTITION BY T1.FNAME,T2.FNAME
                    ORDER BY T0.FNUMBER, T4.FNAME
                ) AS GroupRowNum2,
		        -- 添加分组标识和行号
                ROW_NUMBER() OVER (
                    PARTITION BY T1.FNAME,T2.FNAME, T3.FNAME 
                    ORDER BY T0.FNUMBER, T4.FNAME
                ) AS GroupRowNum3,
		        -- 添加排序列作为计算列
                ROW_NUMBER() OVER (
                    ORDER BY T5.FNAME, T2.FNAME, T3.FNAME, T0.FNUMBER
                ) AS SortOrder
            FROM T_BD_CUSTOMER T0
            JOIN T_BD_CUSTOMERGROUP_L T1 
                ON T1.FID = T0.FPRIMARYGROUP 
                AND T1.FLOCALEID = 2052
            JOIN V_BD_OPERATORGROUP_L T2 
                ON T2.FENTRYID = T0.FSALGROUPID 
                AND T2.FLOCALEID = 2052
            JOIN V_BD_SALESMAN_L T3 
                ON T3.fid = T0.FSELLER 
                AND T3.FLOCALEID = 2052
            JOIN T_BD_CUSTOMER_L T4 
                ON T4.FCUSTID = T0.FCUSTID 
                AND T4.FLOCALEID = 2052
	        JOIN T_BD_DEPARTMENT_L T5 ON T5.FDEPTID=T0.FSALDEPTID AND T5.FLOCALEID=2052
            {depStr}
        ),
        ReceivableData AS (
            SELECT 
		        A.FCUSTID,SUM(A.FBQXL) FBQXL,SUM(A.FBQQTXL) FBQQTXL,SUM(A.FBQQTXE) FBQQTXE,SUM(A.FBQXE) FBQXE
	        FROM (
            SELECT
                T0.FCUSTOMERID AS FCUSTID,
                SUM(CASE WHEN T3.FNAME = '商品料' THEN T11.FSALQTY ELSE 0 END) AS FBQXL,
                SUM(CASE WHEN T3.FNAME = '定制料' THEN T11.FSALQTY ELSE 0 END) AS FBQQTXL,
                SUM(CASE WHEN T3.FNAME = '定制料' THEN T1.FALLAMOUNT ELSE 0 END) AS FBQQTXE,
                SUM(T1.FALLAMOUNT) AS FBQXE
            FROM t_AR_receivable T0
            JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
            JOIN T_AR_RECEIVABLEENTRY_O T11 ON T11.FENTRYID = T1.FENTRYID
            JOIN t_BD_MaterialSale T2 ON T2.FMATERIALID=T1.FMATERIALID
	        JOIN T_BD_MATERIALSALGROUP_L T3 ON T3.FID=T2.FSALGROUP AND T3.FLOCALEID=2052
            WHERE T0.FDATE BETWEEN '{strDate}' AND '{endDate}' AND T0.FSETACCOUNTTYPE=3 AND T11.FISFREE=0
            GROUP BY T0.FCUSTOMERID
            UNION ALL(
		        SELECT 
			        T0.FCONTACTUNIT,
			        0 FBQXL,
			        0 FBQQTXL,
			        0 FBQQTXE,
			        SUM(T1.FAMOUNT) FBQXE
		        FROM T_AR_OtherRecAble T0
		        JOIN T_AR_OtherRecAbleENTRY T1 ON T1.FID=T0.FID
		        WHERE T0.FCONTACTUNITTYPE ='BD_Customer' AND T0.FDATE >= '{strDate}' AND T0.FDATE <= '{endDate}'
		        --AND T0.FCONTACTUNIT=119618
		        GROUP BY T0.FCONTACTUNIT
	        )) A
	        GROUP BY A.FCUSTID
        ),
        ReceiveBillData AS (
            SELECT
                T0.FCONTACTUNIT AS FCUSTID,
                SUM(T1.FREALRECAMOUNT) AS FREALRECAMOUNT
            FROM T_AR_RECEIVEBILL T0
            JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
            WHERE T0.FCONTACTUNITTYPE = 'BD_Customer'
                AND T0.FDATE BETWEEN '{strDate}' AND '{endDate}'
            GROUP BY T0.FCONTACTUNIT
        ),
        OpeningBalance AS (
            SELECT
                COALESCE(A1.FCUSTID, A2.FCUSTID) AS FCUSTID,
                ISNULL(A1.FQCYS, 0) - ISNULL(A2.FQCSK, 0) AS FQCYE
            FROM (
                SELECT TAA.FCUSTID,SUM(TAA.FQCYS ) FQCYS
		        FROM (
                SELECT 
                    T0.FCUSTOMERID AS FCUSTID,
                    T1.FALLAMOUNT AS FQCYS
                FROM t_AR_receivable T0
                JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
                WHERE T0.FDATE < '{strDate}' AND T0.FSETACCOUNTTYPE=3
                UNION ALL
		        SELECT 
			        T0.FCONTACTUNIT,
			        T1.FAMOUNT FQCYS
		        FROM T_AR_OtherRecAble T0
		        JOIN T_AR_OtherRecAbleENTRY T1 ON T1.FID=T0.FID
		        WHERE T0.FCONTACTUNITTYPE ='BD_Customer' AND T0.FDATE < '{strDate}'
		        ) TAA
                GROUP BY TAA.FCUSTID
            ) A1
            FULL JOIN (
                SELECT 
                    T0.FCONTACTUNIT AS FCUSTID,
                    SUM(T1.FREALRECAMOUNT) AS FQCSK
                FROM T_AR_RECEIVEBILL T0
                JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
                WHERE T0.FCONTACTUNITTYPE = 'BD_Customer'
                    AND T0.FDATE < '{strDate}'
                GROUP BY T0.FCONTACTUNIT
            ) A2 ON A2.FCUSTID = A1.FCUSTID
        ),
        AllSK AS(
        SELECT
                COALESCE(A1.FCUSTID, A2.FCUSTID) AS FCUSTID,
                ISNULL(A1.FQCYS, 0) - ISNULL(A2.FQCSK, 0) AS QBSK
            FROM (
                SELECT TAA.FCUSTID,SUM(TAA.FQCYS ) FQCYS
		        FROM (
                SELECT 
                    T0.FCUSTOMERID AS FCUSTID,
                    T1.FALLAMOUNT AS FQCYS
                FROM t_AR_receivable T0
                JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
                WHERE  T0.FSETACCOUNTTYPE=3
                UNION ALL
		        SELECT 
			        T0.FCONTACTUNIT,
			        T1.FAMOUNT FQCYS
		        FROM T_AR_OtherRecAble T0
		        JOIN T_AR_OtherRecAbleENTRY T1 ON T1.FID=T0.FID
		        WHERE T0.FCONTACTUNITTYPE ='BD_Customer' 
		        ) TAA
                GROUP BY TAA.FCUSTID
            ) A1
            FULL JOIN (
                SELECT 
                    T0.FCONTACTUNIT AS FCUSTID,
                    SUM(T1.FREALRECAMOUNT) AS FQCSK
                FROM T_AR_RECEIVEBILL T0
                JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
                WHERE T0.FCONTACTUNITTYPE = 'BD_Customer'
                    --AND T0.FDATE < @STRDATE
                GROUP BY T0.FCONTACTUNIT
            ) A2 ON A2.FCUSTID = A1.FCUSTID
        )
        SELECT
            C.FCUSTID,
            C.FCPLB,
            C.FXSTD,
            C.FXSBM,
            C.FXSY,
	        -- 仅在第一行显示分组信息
            --CASE WHEN GroupRowNum1 = 1 THEN C.FCPLB ELSE NULL END AS FCPLB,
            --CASE WHEN GroupRowNum2 = 1 THEN C.FXSTD ELSE NULL END AS FXSTD,
            --CASE WHEN GroupRowNum3 = 1 THEN C.FXSY ELSE NULL END AS FXSY,
            C.FNUMBER,
            C.FNAME,
            ISNULL(O.FQCYE,0) FQCYE,
            ISNULL(R.FBQXL, 0) AS FBQXL,
            ISNULL(R.FBQQTXL, 0) AS FBQQTXL,
            ISNULL(R.FBQQTXE, 0) AS FBQQTXE,
            ISNULL(R.FBQXE, 0) AS FBQXE,
            ISNULL(RB.FREALRECAMOUNT, 0) AS FBQSKE,
	        ISNULL(O.FQCYE,0)+ISNULL(R.FBQXE, 0)-ISNULL(RB.FREALRECAMOUNT, 0) FBQYE,
	        ISNULL(SYSK.QBSK,0) FDQZQK,C.SortOrder
        INTO #TT1
        FROM BaseCustomer C
        LEFT JOIN ReceivableData R ON R.FCUSTID = C.FCUSTID
        LEFT JOIN ReceiveBillData RB ON RB.FCUSTID = C.FCUSTID
        LEFT JOIN OpeningBalance O ON O.FCUSTID = C.FCUSTID
        LEFT JOIN AllSK SYSK ON SYSK.FCUSTID=C.FCUSTID
        SELECT * FROM #TT1 
        WHERE COALESCE(FQCYE, 0) != 0 
           OR COALESCE(FBQXE, 0) != 0 
           OR COALESCE(FBQQTXE, 0) != 0 
           OR COALESCE(FBQSKE, 0) != 0 
           OR COALESCE(FBQYE, 0) != 0
        ORDER BY SortOrder
        DROP TABLE #TT1
             ");
            DynamicObjectCollection doc = DBUtils.ExecuteDynamicObject(this.Context, sql);
            return doc;
        }
    }
}
